473,467 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Does declaration of a class as 'public' mean anything?

I have been playing around with C#, and rading some books: "Understanding
..NET" by David Chappell, and "Fast Track C#", by Wrox. The Chapell book does
not mention any code that has a class being declared as public, and with no
problems having that class be accessed. The Wrox book says that it is
necessary.

However, my experimentation seems to show that it is not necessary. When I
have this code, I get no compile error, which seems to indicate that there is
no reason to declare it. Also, it seems that it is impossible to declare a
class in anything but public.

And come to think of it, in all my years slamming out C++ code, every class I
composed was declared public. Perhaps the folks at Micro$oft figured to make
it automatic?

I would presume that this is the case for VB.NET. In MC++, it seems to be
impossible to put any visibility (access) qualifier on a __gc class. Is this
accurate?

Thanks

class A { public void f(){}}

public class B { public void g(){}}

class CCC
{
[STAThread]
static void Main(string[] args)
{
A a = new A(); // OK
a.f(); // OK

B b = new B();
b.g();
}
}
Nov 16 '05 #1
3 1941
"Unemployed VC++ guy" <u-****@com.net> wrote in message
news:mq***************@newsread3.news.atl.earthlin k.net...
I have been playing around with C#, and rading some books: "Understanding
.NET" by David Chappell, and "Fast Track C#", by Wrox. The Chapell book does
not mention any code that has a class being declared as public, and with no
problems having that class be accessed. The Wrox book says that it is
necessary.
The best answer here would be to refer you to the C#
reference documentation that ships with the compiler.
It explains what the access and visibility specifiers do.
Sometimes, as you have discovered, 'public' is not
necessary. This is most likely in toy programs that
do not consist of more than one source file (or, more
accurately, one assembly).
However, my experimentation seems to show that it is not necessary.
I would encourage you to rely less on experiments and
more on understanding what your tools are supposed
to do. You can have the bad luck to discover some
misfeature that is really a bug and will go away when
you let experiment results guide your notions of what
is and is not ok. Or you may end up relying on what
is ultimately undefined behavior that just happens to
be usable, for some particular code and one version
of the compiler/library/runtime.
When I
have this code, I get no compile error, which seems to indicate that there is
no reason to declare it.
In the code you showed (and I cut), there was no
reason to specify public visibility. There may well
be a reason for some other collection of code.
Also, it seems that it is impossible to declare a
class in anything but public.
The docs recommended above will disabuse you of
that idea if you read about what 'public' means.
And come to think of it, in all my years slamming out C++ code, every class I
composed was declared public. Perhaps the folks at Micro$oft figured to make
it automatic?
In C++, there is no choice for classes in the global
namespace, or in named namespaces. But if you had
wanted to declare and use a non-public class, you
could have put it in an unnamed namespace.

What the C# designers have done is make it a little
easier to specify what exposure you want and set
a default that is better for real life projects.
I would presume that this is the case for VB.NET. In MC++, it seems to be
impossible to put any visibility (access) qualifier on a __gc class. Is this
accurate?
MC++ follows the same rules as C++ for that sort
of thing. Instead of a qualifier, you use namespaces
to control name placement or, in the case of the
anonymous namespace, visiblity.
Thanks


[toy example cut]

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 16 '05 #2
Unemployed VC++ guy <u-****@com.net> wrote:
I have been playing around with C#, and rading some books: "Understanding
.NET" by David Chappell, and "Fast Track C#", by Wrox. The Chapell book does
not mention any code that has a class being declared as public, and with no
problems having that class be accessed. The Wrox book says that it is
necessary.
If you don't declare a (top-level) class as being public, it will be
internal by default. That means you can only access it within the same
assembly.
However, my experimentation seems to show that it is not necessary. When I
have this code, I get no compile error, which seems to indicate that there is
no reason to declare it. Also, it seems that it is impossible to declare a
class in anything but public.


You get no compile error because it's all in the same assembly. Try
putting A and B in a class library instead, and you'll find that your
calls involving B will work, but those involving A won't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi,

A very nice page to use for your problem

http://msdn.microsoft.com/library/de...tskeywords.asp

Have a look for your question to the section Class Scope (therefore not only
to the top)

I hope this helps,

Cor
Nov 16 '05 #4

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

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
4
by: wim delvaux | last post by:
class A { public : virtual void a( int x ) = 0; virtual void a( void ) { printf( "a without\n" ); } };
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
3
by: pkolinko | last post by:
Hi everyone, I am writing a small low level embedded USB application using C++/CLI windows Forms. I am sort of new to the C++/CLI and having trouble understanding what happens in this very...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
5
by: fomas87 | last post by:
Hi guys, I'm writing a framework for an sdl gui in c++, but got stuck on the circular dependency, I've read a lot of articles about resolving dependencies and about forward declaration but none of...
45
by: Bob Altman | last post by:
This code (Visual Studio 2005) should generate a compilation warning to the effect that I'm accessing a shared member through an instance variable (the "color" variable): Private Sub X(ByVal...
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...
1
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
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...
0
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,...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.