473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Why public method in internal class is allowed

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.

Apr 13 '07 #1
6 14191
On Apr 13, 11:27 am, "Andrus" <kobrule...@hot.eewrote:
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.

Apr 13 '07 #2
On Fri, 13 Apr 2007 08:27:46 -0700, Andrus <ko********@hot.eewrote:
class Class1 {
public void foo() {}
}

Does not cause any error or warning at compile time.
Nor should it.
Why ?
"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
Apr 13 '07 #3
On Apr 13, 12:41 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
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.

Apr 13 '07 #4
On Fri, 13 Apr 2007 13:35:34 -0700, Andy <an***@med-associates.comwrote:
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. :)
Apr 13 '07 #5
On Apr 13, 8:27 am, "Andrus" <kobrule...@hot.eewrote:
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

Apr 14 '07 #6
Jay Riggs <ja*********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
5
by: Wilfried Mestdagh | last post by:
Hi, If you omit private / public / protected etc in a declaration then What is it ? pirvate, protected ? eg: bool test; bool check { return (something == false);
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
5
by: Paul | last post by:
Hello All, I am new to C# and dot NET in general. The book I am reading uses the term "non-public." I suspect that they don't mean "private", otherwise they would have said so. What is the...
13
by: Ben Voigt | last post by:
Is there any way to have an overridden method which is not callable from the assembly overridding it? A contrived example (though I want this feature, my program has nothing to do with food...
17
by: iesvs | last post by:
Hello, i want to create c++ libraries (for my use) but I don't know how I must do to have headers which contain only the public declarations. I used in C to put the private declarations in *.c...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.