简体   繁体   中英

in c# are methods private by default?

If I have a method that does not specify its Accessibility Level will it be Private by 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.

是的,它是私人的。

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/do.net/csharp/programming-guide/classes-and-structs/access-modifiers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM