Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# in VS2005: what do the following types default to in C#?

For C# in VS2005, what will be value of variables of the following types if they are simply declared and not assigned to any value? ie. What are their default values?

int
bool
string
char
enum
like image 837
CJ7 Avatar asked Dec 22 '22 01:12

CJ7


1 Answers

Here's what default value for each type you've mentioned would be.

int = 0  
bool = false  
string = null  
char = '\0'  
enum = 0    //behind the scenes enum is int

Taking this forward, at runtime if you wish to capture default value of any type then you can use default statement in C# and simply call it as following.

//This will print 0 on screen.
Console.WriteLine(default(int));

Generally, this is used in generics for identifying default values of generic type arguments, where the type is only known at runtime.

like image 121
this. __curious_geek Avatar answered Feb 15 '23 22:02

this. __curious_geek