Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of a 'friend' keyword in C Sharp?

What is the equivalent of a 'friend' keyword in C Sharp?

How do I use the 'internal' keyword?

I have read that 'internal' keyword is a replacement for 'friend' in C#.

I am using a DLL in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherited class any way I want. The problem is that most of the code in the parent class has protected methods. Will using a friend somehow make it possible to access or call these protected methods?

like image 555
xarzu Avatar asked Jan 15 '09 02:01

xarzu


People also ask

Is there a friend class for C#?

friend in C#A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Which keyword is used to represent a friend?

Explanation: friend keyword is used to declare a friend function.

Is friend a keyword in C?

friend is a keyword in C++ that is used to share the information of a class that was previously hidden. For example, the private members of a class are hidden from every other class and cannot be modified except through getters or setters.

What is a friend class in C?

A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.


4 Answers

  1. You can use the keyword access modifier internal to declare a type or type member as accessible to code in the same assembly only.

  2. You can use the InternalsVisibleToAttribute class defined in System.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.

like image 148
jason Avatar answered Oct 04 '22 10:10

jason


No, "internal" is not the same as "friend" (at least the C++ 'friend')

friend specifies that this class is only accessible by ONE, particular class.
internal specifies that this class is accessible by ANY class in the assembly.

like image 38
Harley2000 Avatar answered Oct 04 '22 10:10

Harley2000


  1. internal is the C# equivalent of the VB.NET friend keyword, as you have guessed (as opposed to a replacement)

  2. Usage is as follows

    internal void Function() {}
    internal Class Classname() {}
    internal int myInt;
    internal int MyProperty { get; set; }
    
  3. It, basically, is an access modifier that stipulates that the accessibility of the class / function / variable / property marked as internal is as if it were public to the Assembly it is compiled in, and private to any other assemblies

like image 7
johnc Avatar answered Oct 04 '22 11:10

johnc


Your subclass will be able to access the protected members of the class you inherit.

Are you looking to give access to these protected members to another class?

like image 1
strager Avatar answered Oct 04 '22 12:10

strager