Saturday 24 December 2016

Difference between Static variable and Static read only variable in C#

In this post we are going to see what is the difference between Static Variable and Static read only variable in C#, For this to check we are taking a singleton class which can be very useful to discuss.

For Singleton we need a static variable for maintain the singleton instance. Let we see two classes one with static variable another one with static readonly



Static : Static constructor, Normal constructor, properties etc






Static Read Only : Static constructor, other places we can see the Error if user assign









Static :
**********************
In Static variable we can assign the value in static constructor, normal constructor, inside properties etc



    public class PlanetarySystem
    {
        private static PlanetarySystem _instance;

        static PlanetarySystem()
        {
            _instance = new PlanetarySystem();
        }

        public PlanetarySystem(){           
        }

        public static PlanetarySystem Instance
        {
            get
            {
                _instance = new PlanetarySystem();
                return _instance;
            }
        }

        public string PlanetName { set; get; }

        public int Rotation { set; get; }

        public void Rotate()
        {
            Console.WriteLine("Rotating ... ");
        }
      

    }




Static Read only:
*********************
In static readonly we can assign the value only inside of the static constructor, other ways are not allowed.

    public class PlanetarySystem
    {
        private static readonly PlanetarySystem _instance;

        static PlanetarySystem()
        {
            _instance = new PlanetarySystem();
        }

        public PlanetarySystem(){                   
        }

        public static PlanetarySystem Instance
        {
            get
            {             
                return _instance;
            }
        }

        public string PlanetName { set; get; }

        public int Rotation { set; get; }

        public void Rotate()
        {
            Console.WriteLine("Rotating ... ");
        }
      
    }




From this post you can see what is the difference between static and static read only variable

10 comments:

  1. THis is not the correct implementation of the Singleton pattern. Everytime the static instance property is called a new instance is created. There has to be a check to see if the instance is not null to create the instance.

    ReplyDelete
    Replies
    1. This is to show the Difference between the Static variable and Static read only variable, not only the sigleton, for singleton you have to just return the value , instead of creating the instance for the same variable . See the last example where i implemented the singleton class previous are just for difference , where and all static readonly will affect , see the last example for singleton

      Delete
  2. Rafik is correct. Need to check whether static variable is null before creating a new instance. Also may need to consider thread safety. The Lazy class is good for handling this elegantly.

    ReplyDelete
    Replies
    1. This is to show the Difference between the Static variable and Static read only variable, not only the sigleton, for singleton you have to just return the value , instead of creating the instance for the same variable . See the last example where i implemented the singleton class previous are just for difference , where and all static readonly will affect , see the last example for singleton

      Delete
  3. This comment has been removed by the author.

    ReplyDelete


  4. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | top10 dot net training institutes in chennai

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Dot Net Training in Chennai

    ReplyDelete
  6. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet
    effective. A lot of times it’s very hard to get that “perfect balance”
    between superb usability and visual appeal. I must say you’ve done a
    very good job with this.
    oracle certification in Chennai
    asp net training in Chennai
    C # Training in Chennai

    ReplyDelete