Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Nullables are itself a structure and structure cannot contain "Null". So how are we able to assign "Null" values to "Nullable"Types
Posted
Comments
Sergey Alexandrovich Kryukov 19-Apr-13 1:21am    
Pretty interesting question, only your "cannot contain null" considerations are not based on anything. I voted 4 for the question.
—SA
[no name] 19-Apr-13 1:27am    
Hi,

You added comment saying:
"only your 'cannot contain null' considerations are not based on anything"
What does that mean. Is this a wrong Consideration.

Regards,
Mayank Gupta

1 solution

In brief: this is a matter of internal implementation of CLR. There is no a contradiction. Why cannot it contain null. If you define some structure, and add '?' after the type name, it creates new type, not the same as your structure. The idea is well explained here:
http://stackoverflow.com/questions/2503811/how-are-nullable-types-implemented-under-the-hood-in-net[^].

See also: http://msdn.microsoft.com/en-us/library/ms228597%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
[no name] 19-Apr-13 1:30am    
You said that it is matter of Internal Implementation of "CLR" that "Nullable" in-spite of Being a structure can contain null. So why cant we have all the structures implemented this way. Why do we need to define special type "Nullable" to store "Null" Values.
Sergey Alexandrovich Kryukov 19-Apr-13 1:33am    
What do you mean "why"? Because this is new feature of .NET, additional feature of value types. You define new type by just adding '?' to its base type. What's unclear about it?

Why not all structures this way? This is quite clear: because this is absolutely not needed. The nullable value type means a bit of extra resources. Nullable types are rarely used. Why using additional feature and use extra resources when it is not really needed. So '?' types give you more flexible choice. Another reason: there are classes already, nullable because they are reference types.

—SA
[no name] 19-Apr-13 1:40am    
public struct Nullable<t> where T : struct {
private readonly T value;
private readonly bool hasValue;
public Nullable(T value) {
this.value = value;
hasValue = true;
}
public T Value {
get {
if(!hasValue) throw some exception ;-p
return value;
}
}
public T GetValueOrDefault() { return value; }
public bool HasValue {get {return hasValue;}}
public static explicit operator T(Nullable<t> value) {
return value.Value; }
public static implicit operator Nullable<t>(T value) {
return new Nullable<t>(value); }
}

Here the Code says "public struct Nullable<t> where T : struct". Nullable is Implemented this Way and then we have a constructor that says:

public Nullable(T value) {
this.value = value;
hasValue = true;
}

Constructor itself takes a value that is a structure in the contructor, since T is of type "Structure". So how "T" is Taking "Null" Value here
Sergey Alexandrovich Kryukov 19-Apr-13 1:46am    
I don't understand why are you repeating some code from that article. Do you understand that it's a kind of pseudo-code?
No, T is not taking null. It is conducted as the implementation of HasValue...
—SA
[no name] 19-Apr-13 2:02am    
Ya i know "T" is not taking null. But what i wanted to ask is that when we say:

"Nullable<float> obj = null;"

Then the Constructor of "Nullable" will have value as "Null". But the Variable "value" is of type "T" which is a struct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900