Illustrates how to assign default values to fields using initializers : Member Variable « Class « C# / CSharp Tutorial






class PC
{
  public string make = "AAA";
  public string model = "T";
  public string color;
  public int yearBuilt = 1910;

  public void Start()
  {
    System.Console.WriteLine(yearBuilt + " yearBuilt");
  }

}

class MainClass
{

  public static void Main()
  {
    PC myPC = new PC();

    System.Console.WriteLine("myPC.make = " + myPC.make);
    System.Console.WriteLine("myPC.model = " + myPC.model);
    if (myPC.color == null)
    {
      System.Console.WriteLine("myPC.color is null");
    }
    System.Console.WriteLine("myPC.yearBuilt = " + myPC.yearBuilt);
  }
}
myPC.make = AAA
myPC.model = T
myPC.color is null
myPC.yearBuilt = 1910








7.5.Member Variable
7.5.1.fields
7.5.2.A class with method and member variables
7.5.3.field initialization
7.5.4.Add a method to access the field variables
7.5.5.Call base constructor to init member variables
7.5.6.Use this and base together to init a class
7.5.7.How to use a 'has a' relationship
7.5.8.Illustrates how to assign default values to fields using initializers