Jump to content
  • Advertisement
Sign in to follow this  
Stranger

[.net] C# question...

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
In other words if I make a file .CS with only classes these should be public in order to be used by other files?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!