Use Constructor to initialize member variables : Constructor : Class C# Examples


C# Examples » Class » Constructor »

 

Use Constructor to initialize member variables









    
using  System;

class  MyClass
{
    public  int[]  MyIntArray;
    public  int  Y;
    public  int  ObjectCount  =  0;
    
    public  MyClass()
    {
        MyIntArray  =  new  int[10];
        //Do  work  necessary  during  object  creation
    }
    //Overloads  the  MyClass  allowing  you  to  initialize  Y
    public  MyClass(int  myY)
    {
        Y  =  myY;
    }
}

class  MainClass{
    static  void  Main(string[]  args)
    {
        MyClass  X  =  new  MyClass();

        X.ObjectCount++;

        MyClass  YY  =  new  MyClass(10);
    }

}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Constructor