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
Static Read only:
*********************
In static readonly we can assign the value only inside of the static constructor, other ways are not allowed.
}
From this post you can see what is the difference between static and static read only variable
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
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.
ReplyDeleteThis 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
DeleteRafik 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.
ReplyDeleteThis 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
DeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteGiven 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
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.
ReplyDeleteDot Net Training in Chennai
I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
ReplyDeleteData science Course Training in Chennai |Best Data Science Training Institute in Chennai
RPA Course Training in Chennai |Best RPA Training Institute in Chennai
AWS Course Training in Chennai |Best AWS Training Institute in Chennai
Devops Course Training in Chennai |Best Devops Training Institute in Chennai
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet
ReplyDeleteeffective. 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
Nice information. Thanks for sharing
ReplyDeleteasp.net application development company