[.net] C# question...

Started by
5 comments, last by SamLowry 18 years, 10 months ago
Ok I have seen the definition of classes and interfaces either like "class Name" or like "public class Name". Is there a difference between these two in what has to do with accessibility and usage of the class?
Advertisement
edit: woops, see below

[Edited by - Daniel Miller on June 7, 2005 9:55:24 AM]
The default access modifier for top-level classes is internal.
You have two options regarding class accessibility, like you said it: "class Name" or "public class Name".

Using "class Name" behaves similar to "internal" class members - that is, your class can only be used by other classes in the same assembly.

Using "public class Name" in a DLL allows for other assemblies to access that class.

Note that there is no difference between the two when used in executable assemblies (since you can't access any class in an executable assembly outside of the assembly).
Quote:Note that there is no difference between the two when used in executable assemblies (since you can't access any class in an executable assembly outside of the assembly).


This is not true; you can set a reference to an exe but not in Visual Studio .NET. By using the commandline compiler you can reference an exe.

Cheers
In other words if I make a file .CS with only classes these should be public in order to be used by other files?
You can distinguish several levels of "grouping".

There are statements, which you can group in methods.
Methods and fields can be grouped in classes.
Classes can be grouped in class hierarchies.
Hierarchies can be grouped in assemblies.
And assemblies are components of an application.

(Note that the grouping hierarchy is not as strict as I may have made it look: class hierarchies can be spread over multiple assemblies, etc. The grouping mechanisms overlap.)


Often, you want to limit the scope of methods/variables.

If you want something limited to a specific method, you use local variables (there are no nested functions in C#, so you can't have "local functions")
To limit things to a class, you use private.
To limit things to a class hierarchy, you use protected.
To limit things to an assembly, you use internal.


So, if you plan to split up your application into several assemblies (DLL's), you can use internal to limit their visibility to the assembly they belong to.


My answer to your question would depend on who I was giving an answer to. With the information I have, I would say "just make it public", it's not really a big issue.

This topic is closed to new replies.

Advertisement