none
Runtime is unable to call its default constructor , why ??? RRS feed

  • Question

  • I've a question related to .Net Runtime can't call the Type's default consturctor. But i couldn't understand the reason , why it can't call default constructor ??

    The following is an explanation by Jeffrey Richter in one of the MSDN Articles.

    "There are rare situations when the runtime must initialize a value type and is unable to call its default constructor.

    For example, this can happen when a thread local value type must be allocated and initialized when an unmanaged thread first executes managed code. In this situation, the runtime can't call the type's constructor but still ensures that all members are initialized to zero or null. For this reason, it is recommended that you don't define a parameterless constructor on a value type. In fact, the C# compiler (and others) consider this an error and won't compile the code."

    Can somebody explain why can't Runtime call on type's default constructor  with respect to the above example ?

    Thanks,

    Venkataramana M

     

     

     

    Thursday, December 23, 2010 7:38 AM

All replies

  • Do you actually have this issue, or do you just want to know how the .NET framework is initialized from an unmanaged thread? Why would you even want to know this? There are only very rare situations where you would even walk into this issue.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com
    Thursday, December 23, 2010 12:14 PM
  • I don't have any issue.

    But would like to understand , how the .NET framework is initialized from an unmanaged thread?

    Agreed this is a rare issue. But I want to know , why they designed this way  ( not allowing ValueTypes to have default constructors , where as Reference Types can have ) ?

    Thanks,

    Venkataramana M

    Friday, December 24, 2010 7:59 AM
  • I am not exactly sure why it doesn't call the constructor sometimes. The problem is that it is not garantueed (the default constructor being called). Therefore, you shouldn't (and can't) implement a default constructor. When the constructor cannot be invoked by the CLR, it will still make sure the value type has the correct default value as listed in the Default Values Table. But if you had custom code in a constructor, it would not be executed (and you might think it is).


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Friday, December 24, 2010 9:10 AM
  • Dude

    Thanks for the reply.

    Even i'm aware that constructor can't be invoked by the CLR , for that situation only this is there ??

    But why CLR can't invoke the constructor is the question ??? what makes it impossible to invoke it ??

    i'm looking for answer to this.

    Thanks,

    Venkataramana M

    Friday, December 24, 2010 11:18 AM
  •  

    Hi Venkataramana,

     

    Not sure whether it can answer your question, but, as Jeffrey mentioned in his 'CLR via C# -3rd edition', it might be a performance consideration:

     

    ----Chapter 5--------

    Many developers (especially those with a C++ background) would expect the C# compiler to emit code in Rectangle’s constructor that automatically calls Point’s default parameterless constructor for the Rectangle’s two fields.

     

    However, to improve the runtime performance of the application, the C# compiler doesn’t automatically emit this code. In fact, many compilers will never emit code to call a value type’s default constructor automatically, even if the value type offers a parameterless constructor.

     

    To have a value type’s parameterless constructor execute, the developer must add explicit code to call a value type’s constructor.

     


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 28, 2010 3:48 AM
  • May be.

    so if it's a performance consideration , they can apply the same to reference types as well. Isn't it ?

    I'm not quite convinced.

     

    Tuesday, December 28, 2010 9:46 AM
  • Reference types are null by default. Constructors are not called on that anyway. It is really only about value types that have a default value, which is not always initialized via the constructor.

    So, I am convinced :)


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Tuesday, December 28, 2010 10:20 AM
  • Hmm.

    This seems to be a good explanation for my question. Thanks. Even i'm convinced now :-).

     

    Thanks,

    Venkataramana M

    Tuesday, December 28, 2010 12:14 PM
  • One more thing can we get a code example of the situtation ??

    "For example, this can happen when a thread local value type must be allocated and initialized when an unmanaged thread first executes managed code. In this situation, the runtime can't call the type's constructor but still ensures that all members are initialized to zero or null. For this reason, it is recommended that you don't define a parameterless constructor on a value type. In fact, the C# compiler (and others) consider this an error and won't compile the code."

    if so , it would be of great.

    Thanks in advance.

    Thanks,

    Venkataramana M

    Tuesday, December 28, 2010 12:17 PM
  •  

    Hi Venkataramana,

     

    Currently, I cannot find any sample on this topic, however, I will continue to investigate this question, and I will let you know if you get any update.

     

    Thanks for your understanding and support.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 30, 2010 3:36 AM
  • Ok fine. Thanks.

    Will be waiting for your answer then.

     

    Thanks,

    Venkataramana M

    Monday, January 3, 2011 11:24 AM