Why does C# not provide the C++ style 'friend' keyword?
Having friends in programming is more-or-less considered "dirty" and easy to abuse. It breaks the relationships between classes and undermines some fundamental attributes of an OO language.
That being said, it is a nice feature and I've used it plenty of times myself in C++; and would like to use it in C# too. But I bet because of C#'s "pure" OOness (compared to C++'s pseudo OOness) MS decided that because Java has no friend keyword C# shouldn't either (just kidding ;))
On a serious note: internal is not as good as friend but it does get the job done. Remember that it is rare that you will be distributing your code to 3rd party developers not through a DLL; so as long as you and your team know about the internal classes and their use you should be fine.
EDIT Let me clarify how the friend keyword undermines OOP.
Private and protected variables and methods are perhaps one of the most important part of OOP. The idea that objects can hold data or logic that only they can use allows you to write your implementation of functionality independent of your environment - and that your environment cannot alter state information that it is not suited to handle. By using friend you are coupling two classes' implementations together - which is much worse then if you just coupled their interface.
C# equivalent to C++ friend keyword?
Internal
You can use the internal keyword. Your type (or type member) will then only be visible to other types within the same assembly; And also:
If you need your internal types to be visible from other assemblies, you can use the InternalsVisibleToAttribute. This attribute targets your whole assembly and is usually written in the AssemblyInfo.cs file.
PS: Friend keyword doesn't exists in C# but the concept of friendship exists (not exactly the same as the one in C++), it is described on the Friend Assemblies article from the MSDN. Note also that a friend keyword exists in VB.NET and has the exact same behaviour than the C# internal keyword.
What is the C# equivalent of friend?
There's no direct equivalent of "friend" - the closest that's available (and it isn't very close) is InternalsVisibleTo. I've only ever used this attribute for testing - where it's very handy!
Example: To be placed in AssemblyInfo.cs
[assembly: InternalsVisibleTo("OtherAssembly")]
What is the equivalent of a 'friend' keyword in C Sharp?
You can use the keyword access modifier
internal
to declare a type or type member as accessible to code in the same assembly only.You can use the
InternalsVisibleToAttribute
class defined inSystem.Rutime.CompilerServices
to declare a type as accessible to code in the same assembly or a specified assembly only.
You use the first as you use any other access modifier such as private
. To wit:
internal class MyClass {
...
}
You use the second as follows:
[assembly:InternalsVisibleTo("MyFriendAssembly", PublicKey="...")]
internal class MyVisibleClass {
...
}
Both of these can rightly be considered the equivalent of friend
in C#.
Methods that are protected
are already available to derived classes.
When should you use 'friend' in C++?
Firstly (IMO) don't listen to people who say friend
is not useful. It IS useful. In many situations you will have objects with data or functionality that are not intended to be publicly available. This is particularly true of large codebases with many authors who may only be superficially familiar with different areas.
There ARE alternatives to the friend specifier, but often they are cumbersome (cpp-level concrete classes/masked typedefs) or not foolproof (comments or function name conventions).
Onto the answer;
The friend
specifier allows the designated class access to protected data or functionality within the class making the friend statement. For example in the below code anyone may ask a child for their name, but only the mother and the child may change the name.
You can take this simple example further by considering a more complex class such as a Window. Quite likely a Window will have many function/data elements that should not be publicly accessible, but ARE needed by a related class such as a WindowManager.
class Child
{
//Mother class members can access the private parts of class Child.
friend class Mother;
public:
string name( void );
protected:
void setName( string newName );
};
Related Topics
Calling a Function from a String in C#
Random Number Generator Only Generating One Random Number
Deserializing Polymorphic Json Classes Without Type Information Using Json.Net
How to Limit the Amount of Concurrent Async I/O Operations
Why Did I Get the Compile Error "Use of Unassigned Local Variable"
In C#, Difference Between Public, Private, Protected, and Having No Access Modifier
Remove Duplicates in the List Using Linq
How to Save a Stream to a File in C#
How to Read a CSV File into a .Net Datatable
Difference Between the | and || or Operators
How to Update the Gui from Another Thread
How to Call Asynchronous Method from Synchronous Method in C#
How to Save Application Settings in a Windows Forms Application