@miksarus commented on Tue Oct 02 2018
ΠΠ΅ ΠΊΠΎΡΡΠ΅ΠΊΡΠ½ΠΎ ΡΠΊΠ°Π·Π°Π½ΠΎ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΎΡΠΎΠ² Π΄ΠΎΡΡΡΠΏΠ° Π½Π° ΡΡΡΠ°Π½ΠΈΡΠ΅. Private - ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π΄ΠΎΡΡΡΠΏΠ° ΠΎΡΠ½ΠΎΡΡΡΠΈΠΉΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΊ ΡΠ»Π΅Π½Π°ΠΌ ΠΈ ΠΊ ΡΠΈΠΏΡ ΠΏΡΠΈΠΌΠ΅Π½ΡΡΡΡΡ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ. Π’ΠΎ ΠΆΠ΅ ΡΠ°ΠΌΠΎΠ΅ ΠΊΠ°ΡΠ°Π΅ΡΡΡ ΠΈ private protected.
@LisandroSu commented on Tue Oct 02 2018
ΠΠ΄ΡΠ°Π²ΡΡΠ²ΡΠΉΡΠ΅, @miksarus !
ΠΠ»Π°Π³ΠΎΠ΄Π°ΡΠΈΠΌ ΠΠ°Ρ Π·Π° ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½Π½ΡΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ. ΠΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠΎΠΌΠΎΠ³Π°ΡΡ Π½Π°ΠΌ ΠΏΠΎΡΡΠΎΡΠ½Π½ΠΎ ΡΠ»ΡΡΡΠ°ΡΡ ΠΏΠΎΡΡΠ°Π» docs.microsoft.com. ΠΠ΄Π½Π°ΠΊΠΎ Π½Π°ΠΌ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΡ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΠ΅ ΡΠ²Π΅Π΄Π΅Π½ΠΈΡ, ΡΡΠΎΠ±Ρ ΠΏΡΠΎΠ°Π½Π°Π»ΠΈΠ·ΠΈΡΠΎΠ²Π°ΡΡ ΠΠ°Ρ ΠΎΡΠ·ΡΠ². ΠΠ΅ ΠΌΠΎΠ³Π»ΠΈ Π±Ρ ΠΡ Π΄Π°ΡΡ Π±ΠΎΠ»Π΅Π΅ ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΈ ΠΏΠΎΡΡΠ½ΠΈΡΡ ΡΠ²ΠΎΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ?
Π‘ ΡΠ²Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ,
ΠΠ΅ΠΆΠ΄ΡΠ½Π°ΡΠΎΠ΄Π½Π°Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π° MicrosoftΒ DOCS
@miksarus commented on Wed Oct 03 2018
@LisandroSu, ΠΏΡΠΎΡΡ ΠΏΡΠΎΡΠ΅Π½ΠΈΡ. Π― Π΄ΡΠΌΠ°Π» ΠΏΠΈΡΡ ΠΎ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΠΎΠΉ ΡΡΡΠ°Π½ΠΈΡΠ΅, Π° ΠΏΠΎΡ
ΠΎΠΆΠ΅ Π½Π°ΠΏΠΈΡΠ°Π» ΠΎ Π²ΡΠ΅ΠΉ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ.
Π Π΅ΡΡ Π±ΡΠ»Π° ΠΎΠ± ΡΡΠΎΠΌ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ΅: https://docs.microsoft.com/ru-ru/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
Π‘ΠΊΡΠΈΠ½ΡΠΎΡ: https://yadi.sk/i/cA7xBDvI1uW07w
@LisandroSu commented on Wed Oct 03 2018
ΠΠ΄ΡΠ°Π²ΡΡΠ²ΡΠΉΡΠ΅, @miksarus !
Π‘ΠΏΠ°ΡΠΈΠ±ΠΎ Π·Π° ΠΎΡΠ·ΡΠ².
ΠΡ Π°ΠΊΡΠΈΠ²Π½ΠΎ ΠΏΡΠΎΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΠΌ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΈ ΡΠΊΠΎΡΠΎ ΠΎΡΠ²Π΅ΡΠΈΠΌ ΠΠ°ΠΌ.
Π‘ Π½Π°ΠΈΠ»ΡΡΡΠΈΠΌΠΈ ΠΏΠΎΠΆΠ΅Π»Π°Π½ΠΈΡΠΌΠΈ,
ΠΠ΅ΠΆΠ΄ΡΠ½Π°ΡΠΎΠ΄Π½Π°Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π° ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠΈ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΠ°ΠΉΠΊΡΠΎΡΠΎΡΡ
Hi team,
According to the bug reporter, "private" and "private protected" access modifiers can be applied to members only, not to types. Can you please check on this?
Thanks!
@LisandroSu, I have wrote this, because on the page https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/private wrote that private can be applied only for members
VS 2017 say:
"Elements defined in a namespace cannot be explicitly declared as private, protected, protected internal, or private protected"
My experiment screenshot: https://yadi.sk/i/cwOW8T_NvHIyEw
@LisandroSu @miksarus types can be nested, that is, declared inside another type. Nested types can be private or private protected.
The following example (note that classes A and B are declared inside the Program class):
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(new A());
Console.WriteLine(new B());
}
private class A
{
public override string ToString() => "Private class";
}
private protected class B
{
public override string ToString() => "Private protected class";
}
}
compiles and produces the following output:
Private class
Private protected class
There might be a question to following wording:
Private members are accessible only within the body of the class or the struct in which they are declared
What does "member" mean? In particular, does it include type notion? According to the class keyword article:
A class can contain declarations of the following members:
...
Classes
Interfaces
Structs
Enumerations
So, member can be a type.
Moreover, the same page contains the following paragraph:
Class members, including nested classes, can be public, protected internal, protected, internal, private, or private protected.
@pkulikov in my opinion if type was implemented inside another class it is member. it can be called "type-member" (to be honest, i don't know how it can be translated from Russian). This class is nested, so it can be called "nested-type".
In the other hand, our student-junior, was confused after reading the documentation. And this spurred him to make some experimenting with the code to better understand meaning of access modifiers.
Do I understand it correctly that this issue is about the following text (and similar texts for the other modifiers)?
private
The type or member can be accessed only by code in the same class or struct.
I think this text is technically correct, but confusing, because the only case when a type can have the private modifier is when it's also a member.
At the same time, I think the following sections explain this well, and that the distinction about which modifiers can be used in which situations shouldn't be made here.
So, maybe it would be better if the text avoided "type or member" altogether and was changed to something like the following?
private
The target can be accessed only by code in the same class or struct.
I'm not completely happy to use "the target" here, but I could not think of a better word or phrase to use here.
@svick what about dropping "or" and saying
The type member...
Or
The member of a type...
@pkulikov That would not be correct for public and internal, which are also valid for types that are not type members (i.e. non-nested types). And I think it would be better if all modifiers used the same wording in this list.
By the way, wording in the reference section is shorter and avoids using confusing "type or member":
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers
I would remove the whole list and reference the article from the c# reference. Maybe, the whole article can be merged into the reference section, even.