tags:

views:

241

answers:

3

In C++ you can write:

private:
 int w;
 string x;
protected:
 int y;
 string z;

is there something similar in C# ?

+2  A: 

you can't but see this it could help

private int x,y,z; private string x,y,z

peacmaker
Don't do that. Everytime somebody declares more than one variable/field in one line god kills a kitten.
EricSchaefer
@ Eric: lol , you just made my day!
Madi D.
+12  A: 

No, there are no regions with a specific access type in C#. Every member of a class or struct must have an explicit access modifier or accept the default access modifier private.

Also, on the topic of access modifiers in C# compared to C++, C# has two additional modifiers internal and protected internal. The modifier internal means that it is visible only within the defining assembly and protected internal means protected or internal (NOT protected and internal).

Jason
out of interest because I haven't seen protected internal yet: How is "or" different from "and"? Protected Internal = available everywhere within the assembly and available to all derived classes, even in other assemblies? (As opposed to "only to derived classes within the same assembly")
Michael Stum
Thanks for the very elaborate answer :).
Madi D.
@Michael Stum: That's right. If `protected internal` were interpreted as `protected` and `internal` that would mean visible in the defining assembly AND visible to all derived types which together would mean visible to all derived types only in the defining assembly. Interestingly, there is the .NET concepts of `FamilyAndAssembly` and `FamilyOrAssembly`. In C# `protected internal` is effectively `FamilyOrAssembly` and there is no modifier for `FamilyAndAssembly` in C#.
Jason
+1 for both the comment and the answer!
Madi D.
A good way to think about "protected internal" is that the "natural" state of a thing is "private", and each additional access modifier makes it *less private*.
Eric Lippert
@Eric: So is "protected" more or less private than "internal". I think we should forget the "protected" concept it was a mistake. Anything that is protected is just as vulnerable as anything that is public.
Martin York
@Martin York: As `internal` says visible only within this assembly and `protected` says visible only to derived types there is no comparison between `protected` and `internal` as far as which is more `private`. Eric's point is that if you have a member `X` it is by default `private`; adding the modifier `internal` to `X` makes `X` less private (as now it is visible within the defining assembly) and adding the modifier `protected` to `internal X` makes `X` even less private (as now it is also visible to derived types). If `protected internal` was conjunctive this reasoning wouldn't apply.
Jason
@Eric and @Martin I'm also wondering about protected, as most of the time really we (or at least I) want protected virtual, as there isn't that much use for protected without virtual (unless I am missing something). So if we had a time machine, it would be great if we could get rid of the protected modifier and use virtual or even "private virtual" (as opposed to just private) instead. But then again, that would be inconsistent with all other languages. Don't see much use for FamilyAndAssembly either, but I would still love to hear why it wasn't made available to C#/exists in the first place.
Michael Stum
(1) I agree that most of the time, protected methods are virtual. That doesn't mean that we ought to outlaw protected instance methods; a protected field, for example, is never "virtual" but can be useful, so a protected instance method might also be useful. (2) I would love to have private virtual but it has never been a high enough priority of the design team. (3) the accessibility "the more restrictive combination of protected and internal" only protects you from your coworkers misusing your code, and they can already rewrite it. So it doesn't add much value.
Eric Lippert
A: 

You could define your region with #region Private...#endregion , #region Protected...#endregion etc.

lmsasu
That does not have anything near the same semantics as access regions do in C++.
Jason
Well, that has *no* semantics at all. :-)
CesarGon
umh.. my fault, I think a "no, you can't" answer would be better... the question does not say anything about the "semantic meaning" - not to say about misspelling "Private" and "Protected" - C++ is case sensitive, no offence...
lmsasu
i fixed the problem :) and upvoted to fix the down vote :)
Madi D.