By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.