Add statement to the getter and setter of a property : Properties : Class C# Examples


C# Examples » Class » Properties »

 

Add statement to the getter and setter of a property









    
public  class  A
{
      private  int  myValue;

      public  int  MyValue
      {
            get
            {
                  System.Console.WriteLine(  "Getting  myValue"  );
                  return  myValue;
            }

            set
            {
                  System.Console.WriteLine(  "Setting  myValue"  );
                  myValue  =  value;
            }
      }
}

public  class  MainClass
{
      static  void  Main()
      {
            A  obj  =  new  A();

            obj.MyValue  =  1;
            System.Console.WriteLine(  "obj.Value  =  {0}",
                                                                obj.MyValue  );
      }
}
    
   
  
   



Output

Setting myValue
Getting myValue
obj.Value = 1


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Properties