C-sharp: When to mark class and members static

static variable/properties

This is very straight forward, when your object need to share some variable values then make them static. There will be only one memory allocation to that variable which will be accessible for all objects. For more clarity in c#, you can’t access static members using object. You can access it by using class name.

    public class Test
    {
        public int i = 10;
        public static int s = 5;
    }
    static class Program
    {
       
        static void Main()
        {
            Test obj1 = new Test();
            obj1.i = 4;
            Test obj2 = new Test();
            obj2.i = 14;
            //But for static you can't access it by 
            //Object , you can access it directly by class name.
            Test.s = 100;
        }
    }

In above example i have created two object of Test class and for member i two memory block will be allocated while for s only one memory block will be allocated.

static read only

Static read only is static member which values can be initialize only once in program life cycle. Some time it is required, say for example you want to keep your program execution start time which you want to use in other part of you application, then you will never want to change execution start time accidently in that situation you should mark you variable static read only. You can initialize static read only members in static constructor only or at time of declaration as given below.

    public class Test
    {
        public static readonly DateTime execStartTime = DateTime.Now;
        public static readonly int size;
        static Test()
        {
            size = Environment.SystemPageSize;
        }
        public int i = 10;
        public static int s = 5;
        public readonly int k =5;
    }

In above example execStartTime and size will be initialize when first object of Test will be create or any other static member will be referenced.

Note: Difference between const and static read-only is that const is compile time constant which value will never change, while static read-only is run-time constant (you can say) which value will be initialize only once and can’t be changed in program life cycle.

static constructor

Static constructor is constructor which gets called when first object is created or any static member is referenced. It can not take parameters and it has no access modifiers. Please see above example.

static method

When a function is not using any non-static class member then you should mark that function static.

    public class Test
    {
        public static readonly DateTime execStartTime = DateTime.Now;
        public static readonly int size;
        static Test()
        {
            size = Environment.SystemPageSize;
        }
        public int i = 10;
        public static int s = 5;
        public readonly int k =5;
 
        //Can't make static
        public int getiSqr()
        {
            return i * i;
        }
 
        //Marked static because it is not using any
        //class non-static member.
        public static int add(int a, int b)
        { 
            return a+b;
        }
 
        //Marked static because accessing static member
        public static DateTime getExecStartTime()
        {
            return execStartTime;
        }
    }

in above example “add” and “getExecStartTime” are marked static because they are not using any class non-static member while “getiSqrt” is using i hence can not mark static.

static class

When all members of a class is static then you should mark that class static or mark that class sealed and add private constructor, so that not one can inherit or create object for that class. You can do this by just making that class static.

Static class can have only static constructor to initialize static members but it can not have any other constructor.

    public static class Test
    {
        public static readonly DateTime execStartTime = DateTime.Now;
        public static readonly int size;
        static Test()
        {
            size = Environment.SystemPageSize;
        }
        public static int s = 5;
        public static readonly int k =5;
 
        //Marked static because it is not using any
        //class non-static member.
        public static int add(int a, int b)
        { 
            return a+b;
        }
 
        //Marked static because accessing static member
        public static DateTime getExecStartTime()
        {
            return execStartTime;
        }
    }

For more information please see MSDN documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *