Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a "private" or "public" block in C# like in C++? [duplicate]

Tags:

c#

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#?

like image 876
SIMEL Avatar asked Mar 18 '13 15:03

SIMEL


People also ask

What is private 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.

What is the difference between public and private in C#?

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.

What is private set in C#?

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.


1 Answers

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.

like image 72
poitroae Avatar answered Sep 22 '22 14:09

poitroae