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
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |