Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0098 Default value for each type

Class fields are auto-initialized C# initializes the class fields automatically. using System; class Rectangle{ public int Width; public int Height; } class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); Console.WriteLine(r.Width); } } The output: 0 From the output we can see that the Width is initialized to 0. The following table shows the auto-initialized value for each predefined data types: Type Default value Reference types null numeric types 0 enum types 0 char type '\0' bool type false using System; class Test{ public char ch; public bool b; public int i; public string str; } class Program { static void Main(string[] args) { Test t = new Test(); Console.WriteLine(t.ch); Console.WriteLine(t.b); Console.WriteLine(t.i); Console.WriteLine(t.str); } } The output: False 0