Question about TypeInitializationException












0















I have noticed something that I quite don't understand. I have ConfigurationProvider class which loads configuration values from database and then Configuration class which parses them and makes them accessible to the rest of my application. (In reality I parse xml configuration but for example I will just replace it with long)



public class ConfigurationProvider
{
public static ConfigurationProvider Provider { get; } = new ConfigurationProvider();

public string GetParameter()
{
// gets configuration parameter from database
}
}

public class Configuration
{
public static long Parameter { get; } = long.Parse(ConfigurationProvider.Provider.Parameter);
}


In case there is not a valid value in database TypeInitializationException will be thrown when accessing Configuration.Parameter; then when I change value in database and access Configuration.Parameter; again I still get exactly the same exception.



So my question is why is this happening? Can you point me to some documentation of this? As far as I understand public static long Parameter { get; } should be initialized only once, once the class Configuration is accessed for the first time. But exception makes me feel that this initialization failed and should be performed once again. Or does this mean that Parameter actually was initialized and it now references given exception?



ps : I know how to fix this so there is no need for code, I just want to understand this issue better than I do now.










share|improve this question


















  • 1





    If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

    – Mike Zboray
    Nov 16 '18 at 8:11











  • Thank you for clarification.

    – just-my-name
    Nov 16 '18 at 9:02
















0















I have noticed something that I quite don't understand. I have ConfigurationProvider class which loads configuration values from database and then Configuration class which parses them and makes them accessible to the rest of my application. (In reality I parse xml configuration but for example I will just replace it with long)



public class ConfigurationProvider
{
public static ConfigurationProvider Provider { get; } = new ConfigurationProvider();

public string GetParameter()
{
// gets configuration parameter from database
}
}

public class Configuration
{
public static long Parameter { get; } = long.Parse(ConfigurationProvider.Provider.Parameter);
}


In case there is not a valid value in database TypeInitializationException will be thrown when accessing Configuration.Parameter; then when I change value in database and access Configuration.Parameter; again I still get exactly the same exception.



So my question is why is this happening? Can you point me to some documentation of this? As far as I understand public static long Parameter { get; } should be initialized only once, once the class Configuration is accessed for the first time. But exception makes me feel that this initialization failed and should be performed once again. Or does this mean that Parameter actually was initialized and it now references given exception?



ps : I know how to fix this so there is no need for code, I just want to understand this issue better than I do now.










share|improve this question


















  • 1





    If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

    – Mike Zboray
    Nov 16 '18 at 8:11











  • Thank you for clarification.

    – just-my-name
    Nov 16 '18 at 9:02














0












0








0








I have noticed something that I quite don't understand. I have ConfigurationProvider class which loads configuration values from database and then Configuration class which parses them and makes them accessible to the rest of my application. (In reality I parse xml configuration but for example I will just replace it with long)



public class ConfigurationProvider
{
public static ConfigurationProvider Provider { get; } = new ConfigurationProvider();

public string GetParameter()
{
// gets configuration parameter from database
}
}

public class Configuration
{
public static long Parameter { get; } = long.Parse(ConfigurationProvider.Provider.Parameter);
}


In case there is not a valid value in database TypeInitializationException will be thrown when accessing Configuration.Parameter; then when I change value in database and access Configuration.Parameter; again I still get exactly the same exception.



So my question is why is this happening? Can you point me to some documentation of this? As far as I understand public static long Parameter { get; } should be initialized only once, once the class Configuration is accessed for the first time. But exception makes me feel that this initialization failed and should be performed once again. Or does this mean that Parameter actually was initialized and it now references given exception?



ps : I know how to fix this so there is no need for code, I just want to understand this issue better than I do now.










share|improve this question














I have noticed something that I quite don't understand. I have ConfigurationProvider class which loads configuration values from database and then Configuration class which parses them and makes them accessible to the rest of my application. (In reality I parse xml configuration but for example I will just replace it with long)



public class ConfigurationProvider
{
public static ConfigurationProvider Provider { get; } = new ConfigurationProvider();

public string GetParameter()
{
// gets configuration parameter from database
}
}

public class Configuration
{
public static long Parameter { get; } = long.Parse(ConfigurationProvider.Provider.Parameter);
}


In case there is not a valid value in database TypeInitializationException will be thrown when accessing Configuration.Parameter; then when I change value in database and access Configuration.Parameter; again I still get exactly the same exception.



So my question is why is this happening? Can you point me to some documentation of this? As far as I understand public static long Parameter { get; } should be initialized only once, once the class Configuration is accessed for the first time. But exception makes me feel that this initialization failed and should be performed once again. Or does this mean that Parameter actually was initialized and it now references given exception?



ps : I know how to fix this so there is no need for code, I just want to understand this issue better than I do now.







c# exception types static initialization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 7:08









just-my-namejust-my-name

340311




340311








  • 1





    If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

    – Mike Zboray
    Nov 16 '18 at 8:11











  • Thank you for clarification.

    – just-my-name
    Nov 16 '18 at 9:02














  • 1





    If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

    – Mike Zboray
    Nov 16 '18 at 8:11











  • Thank you for clarification.

    – just-my-name
    Nov 16 '18 at 9:02








1




1





If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

– Mike Zboray
Nov 16 '18 at 8:11





If the type initializer throws the runtime caches it and rethrows the exception every time the class is accessed. The initializer isn't actually run again.

– Mike Zboray
Nov 16 '18 at 8:11













Thank you for clarification.

– just-my-name
Nov 16 '18 at 9:02





Thank you for clarification.

– just-my-name
Nov 16 '18 at 9:02












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333027%2fquestion-about-typeinitializationexception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333027%2fquestion-about-typeinitializationexception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine