tags:

views:

252

answers:

4

I have a class which I marked as internal and I marked fields and methods as public. It compiled without errors or warnings. Is there any specific need to have methods as public and class as internal (except when they are being implemented from interfaces or classes)?

+5  A: 

A class marked as internal with public methods would allow for other classes in the same assemly to reference those methods.

internal (C# Reference)

A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code. For example, a framework for building graphical user interfaces could provide Control and Form classes that cooperate using members with internal access. Since these members are internal, they are not exposed to code that is using the framework.

It is an error to reference a type or a member with internal access outside the assembly within which it was defined.

Edit

From Scope of internal method in C#?

You may want a method on a public class which can be called by other classes in the assembly, but not by clients of the assembly.

astander
agreed.Still if i mark the method as internal i am able to refer it inside the assembly.!!
Ravisha
+1  A: 

There's no warning because it doesn't have any adverse effects. The members won't be accessible from other assemblies anyway, whether you mark them as public or internal.

jrcalzada
A: 

Consider this case:

You have a FooFactory creating objects implementing IFoo. Your IFoo implementations may be internal because you don't want to expose them to other assemblies that just use IFoo. The members of the IFoo implementation must be public so they can be used in the other assemblies.

So, as I've now read all of your question, I don't think there's any reason for methods on internal classes to be public unless there's a way for those classes to be accessed from other assemblies either through interfaces or base classes.

Andrew Kennan
Dear andrew.I have mentioned in the question except interface and base class .
Ravisha
Heh... that's what I get for trying to answer questions while a small child tries to steal my mouse...
Andrew Kennan
May be its time u stop playing with mice:)
Ravisha
+7  A: 

It has no adverse effects, and also means that if you ever do decide to make the type public, you won't need to change the accessibility of your members.

Basically for a member:

  • public means the member is visible to anyone who can see the Type.

  • internal means the member is only visible in the current assembly, even if the Type is publicly visible.

So your choice would be based on which of these is most appropriate. In general it's most appropriate to make the members public (i.e. visible to anyone who can see the Type, i.e. part of the Type's public API). You would make members internal for the same reason you make members internal in a public class - typically helper members that should only be visible to "friend" classes in the same assembly, and don't form part of the public API.

In addition, an internal Type can derive from a public Type, so can inherit and override public members. Would it make sense to allow overridden public members, but not new public members?

Joe
Nice explanation Joe ,is there any other use (apart from "f you ever do decide to make the type public, you won't need to change the accessibility of your members.") for having methods public and type internal?
Ravisha
"is there any other use " - there is no difference. So I generally would make members public unless there is an explicit reason to make them internal, i.e. unless they would be internal for a public type (generally helper members that should not be visible in the public API, but are accessible to "friend" classes in the same assembly.
Joe