Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former.

Knowing the Default Access Modifiers in C#

512px-PadlockI have a few friends in college who are learning C# on the side. I've been answering their questions when they ask. One interesting question was regarding access modifiers. I was asked which access modifiers are used by default in certain situations when there is not one specified.

First I'll quickly remind everyone of the access modifiers and what has permission to access them.

private - Any members of the same class have access.

protected - Any members of the same class or any derived class have access.

internal - All code in the same assembly. This means that it doesn't matter where the code is in the assembly. It can be in any class as long as it is inside of the same assembly it has access.

public - EVERYTHING HAS ACCESS!

When you define a data type of some kind (class, struct, enum, etc.) you need not specify an access modifier, but be aware that the default will be internal. This means that anything else in the assembly has access to it. There is very little sense for it to be private, because that would mean that only it has access to itself which means it would never do anything. Since we like the default permission to be as restrictive as possible it makes sense that the default is internal.

large_gold_keyThe other code with access modifiers are the members of the types we were just discussing. Since these are within a type private makes sense for them, and since we like keeping this locked down by default they use the private access modifier if no other is specified. This means they are only accessible by other members of the same thing. So a class might have a private member function and that function can only be called by other members of the class; functions, properties, etc.

Protected is probably my favorite access modifier to use, but since it is less restrictive it is not the default. This is because pretty much any time protected can be used so can private so private will be the default. Public is just way to open to be the default and C# doesn't want to encourage programmers to have everything be public. That is just a bad way to write code.

Don't get locked out because you don't know the default access modifier. Make sure you key in to the correct way of writing your code.

Have fun writing code with access modifiers!

Sponsor

Published Thursday, November 29, 2007 2:21 PM by Brendan

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Knowing the Default Access Modifiers in C# @ Thursday, November 29, 2007 3:42 PM

Not to nitpick but the statement that you made about defalt Access Modifiers is accurate only for top level classes. For nested types the defaults are as follows:

enum - public

class - private

interface - public

struct - private

Source: http://msdn2.microsoft.com/en-us/library/ba0a1yw2(VS.71).aspx

You might also want to note that only public and internal are the only access modifiers allowed on top level classes.

Michael

# re: Knowing the Default Access Modifiers in C# @ Thursday, November 29, 2007 4:25 PM

I was trying not to get too in depth here.

And to nitpick a little bit more now back at you. The only correction you needed to mention was enum and interface being public. Nested classes and structs would qualify when I said "members" since a nested type is basically a member type it is still a member.

Thanks for the comment. I love comments with good information in them. :-)

Brendan

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below