Loading ...

Value type can not contain NULL?

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Value type can not contain NULL?

Value type can not contain NULL?

Posts under the topic: Value type can not contain NULL?

Posted: 10/15/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello Folks,

As I have read that Value type can not contain value Null and Reference type can contain value Null. After this I have read about nullable type. Well Now my question is - I have a int datatype variable int x and its value type.I also have int? y

Can anyone tell me after declare nullable, is still y variable value type? Because I have read value type can not contain NULL. 

 

Thanks in advance.


Posted: 10/15/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

mohit said:

user="mohit kumar"]Can anyone tell me after declare nullable, is still y variable value type?

AFAIK, yes. It's called nullable value types. http://forums.asp.net/t/1551041.aspx

 

 


Posted: 10/15/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

mohit said:

user="mohit kumar"]

Hello Folks,

As I have read that Value type can not contain value Null and Reference type can contain value Null. After this I have read about nullable type. Well Now my question is - I have a int datatype variable int x and its value type.I also have int? y

Can anyone tell me after declare nullable, is still y variable value type? Because I have read value type can not contain NULL. 

 

Thanks in advance.

Mohit, that is very interesting question.

It's true that Value types cannot contain Null value, while Reference types CAN.

When you declare the value type as int? y then its a Nullable Types.

Means, the value type variables can be assigned a null value.

Read more in the link behind the 'Nullable Types' ;) - and you will understand WHY and HOW it is possible.

Hope this helps,
Hajan


Posted: 10/15/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

Vinz said:

user="Vincent Maverick Durano"]

mohit said:

user="mohit kumar"]Can anyone tell me after declare nullable, is still y variable value type?

AFAIK, yes. It's called nullable value types. http://forums.asp.net/t/1551041.aspx

You are correct and you were a bit faster than me :)!


Posted: 10/17/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

First of all yes, the value types can not have null values. Do you know what null means to reference type ? Remember the value of the reference type variable is reference itself. If a reference type has a null value it means it is not pointing to any object. The value of the value types variables is REAL value. You can't have undefined or not existing value in it. Nullable is a kind of struct:

struct Nullable<T> 
{
    bool hasValue;
    T value;
}
and some of the accessors, constructors fields in it. So nullable type you can say is the combination of value type + a bool flag which tells you whether the value type in null or not. When you do the HasValue on the nullable types it checks from this flag only.
Also if you think that because struct is a value type so it will always goes on stack then you are wrong and if someone says you the same that structs always goes on stack then he does not know what he is talking about. In the Microsoft implementation of the desktop CLR local variables of struct type which are not closed-over outer variables of an lambda expressions or anonymous method and are not in an iterator block are stored on the stack rest all goes in heap. I am not going beyond this topic as the current thread is not for this talk. 


tags C#

Posted: 10/17/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thank you all:)

 


Posted: 10/17/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

raghav_khunger said:

user="Raghav Khunger"]

 if you think that because struct is a value type so it will always goes on stack then you are wrong and if someone says you the same that structs always goes on stack then he does not know what he is talking about. 

 

But Sir I have read on internet and books that struct save on stack. Below some MSDN links :-

Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on the stack

Classes are stored in memory via a heap whereas structs are stored in memory via a stack.

Now please tell me how structs are not save on the stack??



Posted: 10/17/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

First of all this statement which you may found :

"Value types are always stored on Stack and reference types are always stored on Heap"

This is not always true. This is a misconception many programmers have. Let's crank and I will expound you - Reference types are stored on Heap . But Value Types are stored depending on the scope. Value types are stored where the instance itself is stored. Reference type objects are always stored on the Heap the same is applicable to static variables. Also value of the Value type is Value itself.

Say here:

int i=5+2;

which is 7 so i variable will contain 7 that's the value itself. The value of reference type is reference not the object to which it refers to.

string str="this is test string";

Now str will contains the reference to the memory block which contains "this is test string" , make note I am saying reference i.e. a pointer which will be pointing to the memory block containing value "this is test string". Also since the string has come into this context I will point out that string is immutable (I will not go beyond on the immutable stuff at this time as orignal question is different) .

Imaging Value type as a book which your friend gave you to read so it means original copy, you here are acting as value type variable containing straight plain value. Now imagine a reference type, say your good friend gave you a link (a URL) to read a nice article. Now here you have reference with you not the value you will have to access the value via URL.

Value types are stored on the heap for the following cases:

  1. If the variables are the fields of the class
  2. If the variables boxed
  3. If the variables are in iterator block
  4. If the variables are acting as Captured Variables

Examples

1.

class MyTestClass
{
    int i;
}

 

2.

int i=2;
object obj=i;

 

3.

static IEnumerable<int> MyTestMethod()
{
    int i = 0;
    yield return i;
}

 

4.

static void MyTestMethod()
{
    int i = 0;

}

 

 

Now come to the structs since they too are value types they will always go where they are declared. Remember the value of variable lives where it is declared so the struct will go where they were declared. If the struct type variable is not the part of class or the conditions (there can be more) discussed above they will be stored on stack else they will be stored of heap. Do not get offended what I discussed above as you will find the same essence when you even will start digging deep into C#. 

 


Posted: 10/17/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

raghav_khunger said:

user="Raghav Khunger"]

Imaging Value type as a book which your friend gave you to read so it means original copy, you here are acting as value type variable containing straight plain value. Now imagine a reference type, say your good friend gave you a link (a URL) to read a nice article. Now here you have reference with you not the value you will have to access the value via URL.

 

This example is very nice to understand:)

 

 


Page 1 of 1 (9 items)