Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former. I have moved my blog to Brendan.Enrick.com.

Syndication

Tags

News

Add to Technorati Favorites

Locations of visitors to this page

All C# Feeds in one place!

View Brendan Enrick's profile on LinkedIn

Brendan Enrick's Facebook profile

Navigation

Archives

Advice Sites

Articles

Blogs

Music

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!

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

# re: Knowing the Default Access Modifiers in C# @ Sunday, August 03, 2008 6:20 AM

I think, default access modifier for class and struct is internal but not private. Could you please clarify

gisha

# re: Knowing the Default Access Modifiers in C# @ Monday, August 04, 2008 8:53 AM

@Gisha Yes, that is correct. It is what I've said here.

"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."

The reason for this is that private means you cannot have access to it outside of the current class, so if a class couldn't be accessed outside of itself that would mean that the class could never be used EVER. That would just be sill to have a class which could never be used for any purpose. This is why the only access modifiers allowed for classes and structs are internal and public.

I hope that clears things up for you.

Brendan

# re: Knowing the Default Access Modifiers in C# @ Wednesday, September 10, 2008 3:07 AM

Thanks you have solved my little problem

Asif Sanaullah

# re: Knowing the Default Access Modifiers in C# @ Thursday, October 23, 2008 5:07 PM

@Michael If you notice the chart you refer to is the default for members of that type. So... interface = internal http://msdn.microsoft.com/en-us/library/ms173121.aspx Interfaces declared directly with a namespace can be declared as public or internal and like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members. Enumeration members are always public, and no access modifiers can be applied.

Nick

# re: Knowing the Default Access Modifiers in C# @ Wednesday, December 31, 2008 7:27 AM

But When we double click button its always protected...why is it so....if possible please mail me my answer

Rahul

# Knowing the Default Access Modifiers in C# @ Thursday, July 09, 2009 7:29 AM

U R WRONG DEAR
SEE THE CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TESTETST
{
public class xyz
{
internal string strName = "Tarique" ;
public static string GetText(string Text)
{
return Text;
}
static void Main(string[] args)
{
Program obj = new Program();

}
}
class Program :xyz
{

static Program()
{
}
public string GetParent()
{
return strName;
}

}
}

TARIQUE

Leave a Comment

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