473,809 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14209
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...@nn owslpianmk.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.comw rote:
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*********@gm ail.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.co m>
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
2328
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 method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
10
5793
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 (also copied below for easier reference): http://groups.google.it/groups?hl=en&lr=&safe=off&selm=6beiuk%24cje%40netlab.cs.rpi.edu&rnum=95 I don't agree anymore.
6
3593
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 myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
5
1872
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
3954
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 example: class A { public: const vector<int>& getTable() { return m_table; }
8
16357
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 either of these methods generally recommended over the other? If yes, why? Thanks!
5
1961
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 difference between "non-public" and "private"? Help is always appreciated. Thanks, Paul
13
3509
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 service): Specifically, how can the DemonstrateConditions method be implemented by GoodEats, so that it can be called by HealthBureau and not by GoodEatsWaiter? Assembly A {
17
2146
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 and the public declaration in *.h. But how to do that in c++. If someone could help me, he would be nice. Thank you.
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.