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

  • Thread starter Unemployed VC++ guy
  • Start date
U

Unemployed VC++ guy

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();
}
}
 
L

Larry Brasfield

Unemployed VC++ guy said:
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.

[toy example cut]
 
J

Jon Skeet [C# MVP]

Unemployed VC++ guy said:
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.
 

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