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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hermes Condez37 reputationPosted Feb 29, 2012, 9:09 PM EST
private int intVariable = 10;
private DateTime today = DateTime.Now;
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 29, 2012, 9:55 PM EST
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.
VulpesRank #55 · 29.2K reputationPosted Feb 29, 2012, 11:57 PM EST
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.