Eli Perpinyal
Eli Perpinyal

Reputation: 1736

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?

Upvotes: 42

Views: 21260

Answers (4)

Lombas
Lombas

Reputation: 1132

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

Upvotes: 0

derek
derek

Reputation: 4886

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

Upvotes: 4

Julien Lebosquain
Julien Lebosquain

Reputation: 41243

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.

Upvotes: 69

Femaref
Femaref

Reputation: 61437

Yes, it is private.

Upvotes: 13

Related Questions