开发者

in c# are methods private by default?

If I have a method that does not specify its Accessibility Level will it be Private by开发者_Go百科 default?

void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    throw new NotImplementedException();
}        

Is the above method private?


It is. The general rule if you don't define any modifier is "the most restricted modifier that can be applied here is used", so private for methods, internal for top-level classes, etc.


Yes, it is private.


For a method inside a class, default is private. It does vary based on the scope of where things are declared, here's an MSDN link with more specifics


Yes, for methods, like your example.

Class and struct members, including nested classes and structs, have private access by default.

But that is not correct for all the access modifiers:

Classes, records, and structs declared directly within a namespace (in other words, that aren't nested within other classes or structs) can be either public or internal. internal is the default if no access modifier is specified.

I consider this a good idea, because by default in OO, everything should be private and only what is really needed should be marked public.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜