Posted: 10/15/2010
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 said: user="mohit kumar"]Can anyone tell me after declare nullable, is still y variable value type?
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
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.
user="mohit kumar"]
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
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
user="Vincent Maverick Durano"]
You are correct and you were a bit faster than me :)!
Posted: 10/17/2010
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; }
Thank you all:)
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.
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??
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:
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#.
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:)