Wednesday 8 June 2016

c# Static and instance

When a field, method, property, event, operator, or constructor declaration includes a static modifier, it declares a static member.

A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.

A static function member (method, property, event, operator, or constructor) does not operate on a specific instance, and it is a compile-time error to refer to this in such a function member.
-------------------------------------------------------------------------------------------------------------------------

When a field, method, property, event, indexer, constructor, or destructor declaration does not include a static modifier, it declares an instance member.

Every instance of a class contains a separate set of all instance fields of the class.

An instance function member (method, property, indexer, instance constructor, or destructor) operates on a given instance of the class, and this instance can be accessed as this
--------------------------------------------------------------------------------------------------------------------------

class Test
{
   int x;
   static int y;
   void F() {
      x = 1;         // Ok, same as this.x = 1
      y = 1;         // Ok, same as Test.y = 1
   }
   static void G() {
      x = 1;         // Error, cannot access this.x
      y = 1;         // Ok, same as Test.y = 1
   }
   static void Main() {
      Test t = new Test();
      t.x = 1;         // Ok
      t.y = 1;         // Error, cannot access static member through instance
      Test.x = 1;      // Error, cannot access instance member through type
      Test.y = 1;      // Ok
   }
}

https://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx

----------------------------------------------------------------------------------------------------
public class IDGenerator
    {
        //1. private static member of the class type
        private static IDGenerator _instance;

        //private static readonly IDGenerator _instance = new IDGenerator();
        private static int value = 0;

        //2. private constructor
        private IDGenerator()
        {
            value++;
        }

        //3. public static method/property that returns the instance
        //instantiating the class if the instance is null
        public static IDGenerator Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new IDGenerator();

                return _instance;
            }
        }

        //this method belongs to the object - requires an instance
        public int NewID()
        {
            var r = new Random();
            var num = r.Next(100000, 999999);
            return num;
        }

        public static int ClassValue
        {
            get { return value; }
        }
    }

----------------------------------------------------------------------------
//form.cs

private void button1_Click(object sender, EventArgs e)
        {
            var id = IDGenerator.Instance.NewID();
            id = IDGenerator.ClassValue;
            label1.Text = id.ToString();
        }

No comments:

Post a Comment