Click here to Skip to main content
11,921,835 members (64,285 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# 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
Pretty interesting question, only your "cannot contain null" considerations are not based on anything. I voted 4 for the question.
mayankgupta688 19-Apr-13 1:27am

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

Mayank Gupta

1 solution

Rate this: bad
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:[^].

See also:[^].

mayankgupta688 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.
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.

mayankgupta688 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
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...
mayankgupta688 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 538
1 Dave Kreskowiak 260
2 Sergey Alexandrovich Kryukov 173
3 phil.o 170
4 RyanDev 134
0 OriginalGriff 7,036
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,286
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2015
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