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
Im thankful for the article post. Thanks Again. Fantastic. Pauletta Hector Sayer
Thank you ever so for you blog article. Thanks Again. Keep writing. Stefania Otto Nee
Some genuinely nice and useful info on this site, besides I think the layout has got fantastic features. Jeanette Skyler Meakem
This article will help the internet visitors for building up new blog or even a weblog from start to end. Kakalina Corey Reinhardt
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
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
Hi there, its pleasant paragraph about media print, we all understand media is a great source of information. Grazia Andie Vitkun
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
Really appreciate you sharing this article. Really thank you! Cool. Magdalene Pavel Ackerley
Thank you for a wonderful in addition to informative post I truly appreciate all the effort that went into the creating. Phebe Urbano Witkin
Hello Dear, are you truly visiting this web site daily, if so then you will absolutely obtain nice experience. Kalie Silvan Kunkle
Very nice article, exactly what I was looking for. Crysta Dru Cutter
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
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
I love this story! I, too, was fascinated by charm bracelets when I was a girl, and very covetous of them. Dorie Justinian Voltz
I truly appreciate this post. Thanks Again. Fantastic. Sammy Ryun Teressa
I like reading through a post that can make people think. Also, thanks for allowing me to comment. Fara Corrie Vanthe
At least most of the restructuring of the other garden was completed last year. Marcia Rafaello Katerine
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
Your photographs are amazing!! Keep up the great work. Jonell Nester Etrem
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.
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.
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.
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.
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.
Great article! We are linking to this particularly great post on our website. Julina Micheil Olodort