Illustrates how to assign default values to fields using initializers : Member Variable : Class C# Examples


C# Examples » Class » Member Variable »

 

Illustrates how to assign default values to fields using initializers









    
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);
    }
}
    
   
  
   



Output

myPC.make = AAA
myPC.model = T
myPC.color is null
myPC.yearBuilt = 1910


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Member Variable