A simple property example : Properties : Class C# Examples


C# Examples » Class » Properties »

 

A simple property example









    
using  System;  
  
class  SimpProp  {    
    int  prop;  
  
    public  SimpProp()  {  
          prop  =  0;  
    }  
  
    public  int  MyProp  {  
        get  {  
            return  prop;  
        }  
        set  {  
            prop  =  value;  
        }    
    }  
}    
    
class  MainClass  {    
    public  static  void  Main()  {    
        SimpProp  ob  =  new  SimpProp();  
  
        Console.WriteLine("Original  value  of  ob.MyProp:  "  +  ob.MyProp);  
  
        ob.MyProp  =  100;
        Console.WriteLine("Value  of  ob.MyProp:  "  +  ob.MyProp);  
  
        Console.WriteLine("Attempting  to  assign  -10  to  ob.MyProp");  
        ob.MyProp  =  -10;  
        Console.WriteLine("Value  of  ob.MyProp:  "  +  ob.MyProp);  
    }  
}
    
   
  
   



Output

Original value of ob.MyProp: 0
Value of ob.MyProp: 100
Attempting to assign -10 to ob.MyProp
Value of ob.MyProp: -10


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Properties