views:

211

answers:

4

Im not sure what they're called but im referring to the

private:
public:
protected:
    float bla1;
    float bla2;
    float bla3;

keywords in c++. Is there an equivalent in c#? It seems rather tedious having to repeat yourself! ala

protected float bla1;
protected float bla2;
protected float bla3;
+8  A: 

No there isn't such a thing. In fact, it's designed to be like that to make code more readable. This applies to both C# and Java.

Mehrdad Afshari
+5  A: 

It's worth noting that if you have several members of the same type, you can declare them as:

protected float bla1, bla2, bla3;

HTH, Kent

Kent Boogaart
+9  A: 

No. The access is specified on each declaration.

The benefit of this is that a method's location within the source file has no effect on the behaviour. That means you can move methods and properties around (e.g. to cluster related methods together) with impunity. The same isn't quite true of fields - it's possible to make the declaration order of fields matter. Admittedly it's best not to do that in the first place...

Jon Skeet
Nice point. This feature helps a lot when merging different versions of a file to check in.
Mehrdad Afshari
+1  A: 

No there is no equivalent in C# (VB and F# as well).

Personally I love this difference. I work in a very large C++ code base and there is no way to look at a particular method and know it's particular accessibility. Some of the classes have gotten so large that it takes a significant amount of page scrolling just to see the modifier.

Some coders may think that's not to bad but consider what happens when people start mixing in #if defs in the middle of the class and adding modifiers within those #if's. It makes determining the access modifier during a code review a non-trivial operation.

It's a small typing sacrifice to add the modifier inline but worth it in terms of long term readability.

JaredPar