Practical Uses for the "Internal" Keyword in C#

Practical uses for the internal keyword in C#

Utility or helper classes/methods that you would like to access from many other classes within the same assembly, but that you want to ensure code in other assemblies can't access.

From MSDN (via archive.org):

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.

You can also use the internal modifier along with the InternalsVisibleTo assembly level attribute to create "friend" assemblies that are granted special access to the target assembly internal classes.

This can be useful for creation of unit testing assemblies that are then allowed to call internal members of the assembly to be tested. Of course no other assemblies are granted this level of access, so when you release your system, encapsulation is maintained.

Why is the internal keyword used in this example from Jon Skeet's book?

Before going any deeper in the posted code, you may review the MSDN Link on access modifiers in .Net C#.

Idea remains we make something internal when we want that to be accessed only within the assembly (mostly dll). It may seem similar to public, but usage is quite different, since for public its an open access for all the callers from anywhere.

In the code sample the usage of internal has a different perspective, this is more to control the object of the IterationSampleIterator class, by making the constructor internal, so this class can only be instantiated from the callers within its own assembly, but any other caller from outside cannot do it.

For all the users outside the assembly they can only call the methods like property like Current or Methods like MoveNext and Reset to work with internal implementation there's no direct access to the object.

Normally such would be the case where within assembly there are certain classes which internally use and thus expose the wrapped functionality. I think System.IO is one of the assemblies exposing such classes, with can be used within the assembly boundaries

internal keyword in C#

I have a c# static class that I have declared as internal. I also declared a static property in it. I declared it as public. Now when a public property is encapsulated in an internal class does it limit its scope or not.

To be strictly correct, no. The scope of an entity is defined as the region of program text in which that entity may be referred to by its unqualified name. The scope of the property is the same regardless of the accessibility domain of the class; the property's scope includes the body of the class and those of any subclasses.

The question you actually intended to ask was "what is the accessibility domain of a public property of an internal class?" The accessibility domain of an entity is the region of program text in which that entity is accessible. The answer to that is "the accessibility domain of a public property of an internal class is the same as the accessibility domain of the class". The accessibility domain of an internal class is the program in which it is declared.

Internal vs. Private Access Modifiers

internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll)

private is for class scope (i.e. accessible only from code in the same class).

Why use a public method in an internal class?

UPDATE: This question was the subject of my blog in September 2014. Thanks for the great question!

There is considerable debate on this question even within the compiler team itself.

First off, it's wise to understand the rules. A public member of a class or struct is a member that is accessible to anything that can access the containing type. So a public member of an internal class is effectively internal.

So now, given an internal class, should its members that you wish to access in the assembly be marked as public or internal?

My opinion is: mark such members as public.

I use "public" to mean "this member is not an implementation detail". A protected member is an implementation detail; there is something about it that is going to be needed to make a derived class work. An internal member is an implementation detail; something else internal to this assembly needs the member in order to work correctly. A public member says "this member represents the key, documented functionality provided by this object."

Basically, my attitude is: suppose I decided to make this internal class into a public class. In order to do that, I want to change exactly one thing: the accessibility of the class. If turning an internal class into a public class means that I have to also turn an internal member into a public member, then that member was part of the public surface area of the class, and it should have been public in the first place.

Other people disagree. There is a contingent that says that they want to be able to glance at the declaration of a member and immediately know whether it is going to be called only from internal code.

Unfortunately, that doesn't always work out nicely; for example, an internal class that implements an internal interface still has to have the implementing members marked as public, because they are part of the public surface of the class.

Why do i have access to internal methods outside of the namespace?

As per the documentation internal (C# Reference)

Internal types or members are accessible only within files in
the same assembly

As per your comment

[It's] difficult to design a way to hide these functions but still give
access to a specific class.

The standard access modifiers are fairly limited, you would need put the calling code in the same assembly to use internal. Additionally there is no way to grant an access list for calling classes unless you do this at runtime.

However, you could use an Explicit Interface Implementation. This will not completely limit access, but it will make it so you need to explicitly ask for it, and hides it any other time.

public interface IGrid
{
Vector2Int ToGrid(...);
}

public abstract class Grid : IGrid
{
Vector2Int IGrid.ToGrid(...) {}
}

Usage

var rectGrid = new RectGrid();
((IGrid)rectGrid).ToGrid(); // you need to explicitly cast to the interface

Why do local functions have the internal access modifier?

Looks like code reuse artifact.
Roslyn probably uses same code to handle local functions and lambdas, but injects method definitions in different classes.

In case of lambda it injects lambda body to generated closure (DisplayClass) class and it should be internal to be referenced from calling function.

What is internal set property in c#?

If you have a property with an internal set accessor (and public get accessor) it means that code within the assembly can read (get) and write (set) the property, but other code can only read it.

You can derive the above information by reading about the internal access modifier, the public access modifier and properties.

Also, you can read about Restricting Accessor Accessibility.

Should methods in a web app be public or internal?

In general public should be limited to externally visible methods - this includes public API and methods that must be exposed dues to technical restrictions (i.e. classes for ASP.Net pages).

In case of web application there is generally no "public API" as such libraries generally are not expected to be consumed by external users.

So mostly there is no practical differences between internal and public for web application development. You gain some minor convenience for using only public as you no longer need to have InternalsVisibleTo attributes for unit tests.

More discussions: public vs. internal methods on an internal class, internal vs public in c#

Side note: ASP.Net MVC web site is a class library - so all discussions about access modifiers related to class libraries applies to ASP.Net MVC/WebAPI sites.



Related Topics



Leave a reply



Submit