Click here to Skip to main content
13,045,285 members (61,739 online)
Rate this:
Please Sign up or sign in to 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 18-Apr-13 19: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<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
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<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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2017
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