Why public method in internal class is allowed

A

Andrus

class Class1 {
public void foo() {}
}


Does not cause any error or warning at compile time.

Why ?

public access modifier should not allowed in foo() since it is member of
internal class

Andrus.
 
A

Andy

class Class1 {
public void foo() {}
}

Does not cause any error or warning at compile time.

Why ?

public access modifier should not allowed in foo() since it is member of
internal class

Andrus.

Well if the class won't be seen regardless of what the modifier on foo
is, does it really matter? The end result is the same; only internal
members may use the class.
 
P

Peter Duniho

class Class1 {
public void foo() {}
}

Does not cause any error or warning at compile time.

Nor should it.

"Internal" only restricts access to code within files from the same
assembly. But it's *in addition to* any existing access modifiers.
Without "public", an identifier would still not be accessible even within
files from the same assembly (except for places that would have access
anyway, such as code within the same class).
public access modifier should not allowed in foo() since it is member
of internal class

Well, that's just not true. I hope the above explains why.

Pete
 
A

Andy

Without "public", an identifier would still not be accessible even within
files from the same assembly (except for places that would have access
anyway, such as code within the same class).

That's not true; I use internal all the time on members of a type
without specfying public, protected or private, and my other classes
within the assembly can still use the member.
 
P

Peter Duniho

That's not true; I use internal all the time on members of a type
without specfying public, protected or private, and my other classes
within the assembly can still use the member.

Sorry, you're right. I got distracted from my main point which is that
it's not an *error* to include "public". Should have stuck to that. :)
 
J

Jay Riggs

class Class1 {
public void foo() {}
}

Does not cause any error or warning at compile time.

Why ?

public access modifier should not allowed in foo() since it is member of
internal class

Andrus.

Andrus,

Write a small test program and take a look at the IL; you'll see that
classes without a defined scope are made public.

-Jay
 
J

Jon Skeet [C# MVP]

Jay Riggs said:
Write a small test program and take a look at the IL; you'll see that
classes without a defined scope are made public.

No they're not, they're internal for top-level classes, and private for
nested classes. The C# rule is that the default is always the most
private applicable access.

Example - I compiled this C#:

class Test
{
static void Main()
{
}
}

and the resulting IL for the class was:

..class private auto ansi beforefieldinit Test
extends [mscorlib]System.Object
{
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Test::Main

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Test::.ctor

} // end of class Test

The "private" of the class here actually means C#'s "internal" - top
level classes can only be public or private as far as IL is concerned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top