Tuesday, August 26, 2014

Understanding Static Constructor (C# reference)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps
{

    class statiConstructor
    {
        private static int a;
        static statiConstructor()
        {
            a = 10;
            Console.WriteLine("Static Constructor with static member value=" + a);
        }
        public static void MethodCall()
        {
            Console.WriteLine("Method Call ");
        }
    }
    class UnderstandingStaticConstructor
    {
        public static void Main()
        {
         // In particular, a static constructor is called once, automatically, when the class is used for the first time.
            statiConstructor k = new statiConstructor();

            statiConstructor.MethodCall();
            statiConstructor.MethodCall();

        }
    }
}



Output:

Static Constructor with static member value = 10
Method Call
Method Call



No comments:

Post a Comment