can we assign null value to struct type of variable?
struct MyStruct
{
}
MyStruct var = null;
is this is possible in C# .net?
if not ? then how C# is allowing Nullable < T > struct type of variable can be assigned as null?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
On the subject of:
The answer is actually quite simple: The C# compiler knows about
Nullable<T>and handles it as a special case to maintain the illusion.For example, this:
Actually compiles into this:
Virtually all of this magic is handled by the compiler — .NET mostly views
Nullable<T>as just another ValueType. I remember reading (most likely from Eric Lippert’s blog), that late in the development process they discovered one edge issue that just could not be handled by compiler tricks alone. The runtime folks agreed to make one change that treatedNullable<T>as a special case. Unfortunately, I don’t remember what the special case was and I can’t find the reference anymore.Please review this article for details (especially the second comment which is by Lippert):
http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx