Keyword readonly and static in C#

Access restriction of a variable in a class is a broad topic in C#; we define the restriction by adding the keyword in front of our variables.

In some cases, we may have some member or variables in our class, and we don’t want other objects or classes amend their value. In this case, keyword readonly and static are very handy.

Between keyword readonly and static, static is more strict compared with readonly.


Case Study

We define a Book class and the Book class has a name property and category property. We will pay more attention on the category member in this case.

Keyword – Read-Only

We add a keyword readonly in front of category.

public string Name 
{
get;
set;
}

readonly string category = "Science";

public Book(string name)
{
Name = name;
category = "";
}

The code above is perfectly legal, we assign variable category as Science in the beginning. We can assign another value when we run the class initialiser. However, if we try to amend the value of category outside of the initialiser, complier will issue an error; because it has a keyword readonly.

For example, the following code is illegal; because we try to amend the category outside of the initialiser.

public string Name 
{
get;
set;
}

readonly string category = "Science";

public Book(string name)
{
Name = name;
category = "";
}

public void AddRate(char letter) 
{
category = ""; //It is illegal, as we try to amend readonly vairble outside of the initialiser.
}

Keyword – static

Compared with keyword readonly, keyword static is more strict.

The following code is illegal, as category is not a variable anymore; because we add a keyword static in front of category property.

public string Name 
{
get;
set;
}

static string category = "Science";

public Book(string name)
{
Name = name;
category = ""; //It is illegal, as constant category is no longer a variable anymore
}

In another word, if we declare a member as constant, we can’t change it anymore. If we need to access the member outside our class, we need to access by type name, instead of object name.

For example, we need to access the value of category outside of the class. The following code is perfectly legal.

Book.category; //Legal

Whereas, the following code is illegal.

book.category; //Illegal


26 Replies to “Keyword readonly and static in C#”

  1. If you desire to increase your familiarity simply keep visiting this website and be updated with the latest news update posted here. Linzy Husain Christabella

  2. Just beneath, are numerous absolutely not connected web pages to ours, on the other hand, they may be surely really worth going over. Sally Ciro Agata

  3. Great tips, Lisa. I always ask for a deposit for larger projects and I even consider doing so for smaller projects as well. Prissie Donnell Orferd

  4. Thank you for a wonderful in addition to informative post I truly appreciate all the effort that went into the creating. Phebe Urbano Witkin

  5. Practic noi romanii am pierdut 24 de ani de pomana, numai pentru a ne intoarce economic in timp, chiar in anul de gratie 1945. Jilly Mathias Alexandrina

  6. You made some clear points there. I did a search on the issue and found most people will consent with your blog. Trisha Hebert Demetri

  7. If some one wants expert view about running a blog then i recommend him/her to go to see this webpage, Keep up the good work. Annadiane Swen Armillas

  8. A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful steps on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a pretty cool job.

  9. Needed to write you this very small word to help thank you very much yet again for the fantastic information you’ve discussed at this time. It has been certainly particularly generous of people like you to allow extensively all that a lot of people could possibly have distributed as an e-book to earn some cash for their own end, especially considering the fact that you could have done it if you considered necessary. The good tips also acted like a easy way to be sure that most people have a similar dreams just like mine to grasp a whole lot more in respect of this condition. I’m certain there are lots of more enjoyable moments ahead for individuals that find out your blog.

  10. I am only writing to make you understand of the exceptional discovery my cousin’s child enjoyed going through your blog. She discovered a wide variety of details, most notably how it is like to have a very effective coaching heart to get many more effortlessly fully understand certain extremely tough matters. You actually did more than my desires. I appreciate you for displaying such great, healthy, educational and also cool guidance on that topic to Evelyn.

  11. I am glad for writing to make you know what a nice encounter my wife’s girl had studying your web site. She mastered several pieces, which include what it’s like to possess an awesome helping nature to let other individuals with ease know precisely some problematic subject matter. You truly exceeded people’s expectations. Thank you for producing these practical, trusted, informative and in addition fun tips about this topic to Kate.

  12. Thanks for the advice on credit repair on this excellent site. Some tips i would tell people should be to give up the particular mentality that they may buy currently and shell out later. Being a society many of us tend to try this for many issues. This includes vacation trips, furniture, along with items we wish. However, you’ll want to separate your current wants out of the needs. If you are working to boost your credit score actually you need some trade-offs. For example you’ll be able to shop online to save cash or you can look at second hand merchants instead of high-priced department stores pertaining to clothing.

Leave a Reply

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