What is the C# equivalent of C++ friend keyword?


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.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

C++ equivalent of friend in C#

The closest equivalent is to create a nested class that will access the outer class private members.

Here, the inner class can access the Outer class private members −

class Outer {
   class Inner {

   }

}

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements