In C++ it's possible to create a "block" of members by privacy:
private :
int var1;
char var2;
...
public :
int var3;
char var4;
...
I have tried to find examples of the same in C#, but couldn't find one. I tried to write such a block in Visual Studio, but I got an error.
Is there a similar syntax that is valid in C#?
When preceding a list of class members, the private keyword specifies that those members are accessible only from member functions and friends of the class. This applies to all members declared up to the next access specifier or the end of the class.
public - can be access by anyone anywhere. private - can only be accessed from with in the class it is a part of. protected - can only be accessed from with in the class or any object that inherits off of the class.
An immutable class is a class that doesn't change once it's created, so private setters (or no setters at all) is needed to protect the properties. Private setters came into more frequent use with the property shorthand that was instroduced in C# 3.
The short answer is no, there isn't. You always should write the access specifier in front of your field/method.
public int var1;
public char var2;
Note that private
is the default specifier. It is a question of design, but I would always explicitly designate the modifier. (And even if it is just for the consistent indentation!)
Read more on Accessibility Levels (C#) at msdn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With