Declare class fields and methods : Object Reference : Class C# Examples


C# Examples » Class » Object Reference »

 

Declare class fields and methods






The new operator dynamically allocates memory for an object and returns a reference to it.
This reference is, more or less, the address in memory of the object allocated by new.
This reference is then stored in a variable.





    
public  class  House
{
    public  string  make;
    public  string  model;
    public  string  color;
    public  int  yearBuilt;

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

    public  void  Stop()
    {
        System.Console.WriteLine(model  +  "  stopped");
    }

}

class  MainClass
{

    public  static  void  Main()
    {
        
        House  myHouse;

        System.Console.WriteLine("Creating  a  House  object  and  assigning  its  memory  location  to  myHouse");
        myHouse  =  new  House();

    }

}
    
   
  
   



Output

Creating a House object and assigning its memory location to myHouse


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Object Reference