warning about public access specifier by Krzysztof Cwalina

A

Agents Marlow

I am new to C# (but not to programming, I have been using C++ for many
years). I have started reading "The C# Programming Language" and have
got to the bit on access specifiers. I am trying to understand the
comment made by Krzysztof Cwalina:

People need to be careful with the public keyword. public in C# is not
equivalent to public in C++! In C++, it means “internal to my
compilation unit.” In C#, it means what extern meant in C++ (i.e.,
everybody can call it). This is a huge difference!

Can someone explain this please?

Is it to do with symbol visibility? Does public mean externally
visible to the known universe? Not every class needs to have such
visibility but classes that cooperate do need some sort of visibility
among themselves. What sort of access would this be when public would
be too much? Would that be 'internal'? I am struggling to understand
this, any help would be greatly appreciated.

Regards,

Andrew Marlow
http://www.andrewpetermarlow.co.uk
 
M

michael.boehnisch

Agents said:
People need to be careful with the public keyword. public in C# is not
equivalent to public in C++! In C++, it means “internal to my
compilation unit.” In C#, it means what extern meant in C++ (i.e.,
everybody can call it). This is a huge difference!

The reason is a different library concept. In C#, to make something visiblefrom a library you only need to declare it as public on namespace level (apublic member of an internal class is still invisible). This will expose the names and signatures of affected symbols, plus the implementation. Othercode merely needs to reference the assembly (library) compiled from this to make use of the public entities (and yes, they are *all* visible).

In C++ to call code from a library you need corresponding declarations as source code *and* the entities must be exposed from the compiled library using platform specific means, e.g. non-standard keywords like __declspec( dllexport ) decorating the identifiers in library source code, or special tools (library aware linkers or archivers) creating binary archives that exposeselected symbols. Both are unrelated to the public keyword as it affects only the scope in the source code under compilation. The implementor may choose to expose a private member of a class in a library and nothing keeps source code from declaring it as public and using it as such - unless the library format provides some sort of protection.

As a side note, the "extern" keyword is also unrelated to this issue; it merely marks a class or function as being imported from a library so you can use it without providing an implementation in your own source code.
Is it to do with symbol visibility? Does public mean externally
visible to the known universe?

In C# on namespace level: yes.
Not every class needs to have such
visibility but classes that cooperate do need some sort of visibility
among themselves.
Right.

What sort of access would this be when public would
be too much? Would that be 'internal'? I am struggling to understand
this, any help would be greatly appreciated.

If you don't decorate a class in C# at all or decorate it using the "internal" keyword, it is exposed only to its surrounding compilation unit, i.e. the project implementing the library.

MiB.
 
A

Arne Vajhøj

I am new to C# (but not to programming, I have been using C++ for many
years). I have started reading "The C# Programming Language" and have
got to the bit on access specifiers. I am trying to understand the
comment made by Krzysztof Cwalina:

People need to be careful with the public keyword. public in C# is not
equivalent to public in C++! In C++, it means “internal to my
compilation unit.” In C#, it means what extern meant in C++ (i.e.,
everybody can call it). This is a huge difference!

Can someone explain this please?

The author has gotten both public and extern in C++ wrong.

It is unexplainable.
Is it to do with symbol visibility?
Yes.

Does public mean externally
visible to the known universe?

Yes. With the restriction that the known universe may not have
the assembly containing it.
Not every class needs to have such
visibility but classes that cooperate do need some sort of visibility
among themselves. What sort of access would this be when public would
be too much? Would that be 'internal'?

Probably. That restricts visibility to the assembly and that
is a desirable visibility in many cases.

Arne
 

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