Hi friends,
I want to know that how to initialize a default value when you know it's data type? Please give an example how to do that?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 1, 2012, 4:57 AM
Briefly, this is:
zero in the case of numbers and enums
false in the case of bool
'\0' in the case of char
the value found by setting all bytes to zero in the case of other structs
null for all reference types including string
In general it can be found using the expression default(T) where T is the type.
If this default value happens to coincide with what you want the field's initial value to be, then there's no need to specify it. For example:
private bool bVariable; // false by default
private DateTime someDate; // 00:00:00.0000000, January 1, 0001 by default
However, you can do so if you want in the interests of clarity.
Jignesh TrivediPosted Mar 1, 2012, 2:55 AM
when you know it's data type, Default value can be set by
string value = "This is my Test";
but your value can not be change than use readnly or const.
hope this help.
Hermes CondezPosted Mar 1, 2012, 2:09 AM
private int intVariable = 10;
private DateTime today = DateTime.Now;