Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Nullable
Nullables are itself a structure and structure cannot contain "Null". So how are we able to assign "Null" values to "Nullable"Types
Posted 18-Apr-13 20:12pm
Comments
Sergey Alexandrovich Kryukov at 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
mayankgupta688 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
mayankgupta688 at 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 at 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
mayankgupta688 at 19-Apr-13 1:40am
   
public struct Nullable 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 value) {
return value.Value; }
public static implicit operator Nullable(T value) {
return new Nullable(value); }
}
 
Here the Code says "public struct Nullable 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 at 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
mayankgupta688 at 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 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)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100