Using Methods to change private fields : private « Class « C# / CSharp Tutorial






using System;

  class Class1
  {
    static void Main(string[] args)
    {
            MyCar car = new MyCar();
            int refChan = 0;
      int chan = 0;

            car.GetSpeed( chan );
            car.GetSpeed( ref refChan );
    }
  }
  public class MyCar
  {
        private static int speed = 2;
        private const int maxSpeed = 200;
        
        public bool ChangeSpeed(int newSpeed)
        {
            if( newSpeed > maxSpeed )
                return false;

            speed = newSpeed;

            return true;
        }
        public void GetSpeed( int param )
        {
            param = speed;
        }

        public void GetSpeed( ref int param )
        {
            param = speed;
        }
  }








7.16.private
7.16.1.Define private constructors to make a singleton
7.16.2.Use Properties to get and set private member variable
7.16.3.private static and const Fields
7.16.4.Using Methods to change private fields