Just curious.
If you go:
string myString;
Its value is null.
But if you go:
int myInt;
What is the value of this variable in C#?
Thanks
David
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Firstly, note that this is only applicable for fields, not local variables – those can’t be read until they’ve been assigned, at least within C#. In fact the CLR initializes stack frames to 0 if you have an appropriate flag set – which I believe it is by default. It’s rarely observable though – you have to go through some grotty hacks.
The default value of
intis 0 – and for any type, it’s essentially the value represented by a bit pattern full of zeroes. For a value type this is the equivalent of calling the parameterless constructor, and for a reference type this is null.Basically the CLR wipes the memory clean with zeroes.
This is also the value given by
default(SomeType)for any type.