Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 721 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does C# not provide the C++ style 'friend' keyword?

#1
The [C++ friend keyword][1] allows a `class A` to designate `class B` as its friend. This allows `Class B` to access the `private`/`protected` members of `class A`.

I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this [earlier StackOverflow question](

[To see links please register here]

) seem to be saying it is a useful part of C++ and there are good reasons to use it. In my experience I'd have to agree.

Another [question](

[To see links please register here]

) seems to me to be really asking how to do something similar to `friend` in a C# application. While the answers generally revolve around nested classes, it doesn't seem quite as elegant as using the `friend` keyword.

The original [Design Patterns book](

[To see links please register here]

) uses it regularly throughout its examples.

So in summary, why is `friend` missing from C#, and what is the "best practice" way (or ways) of simulating it in C#?

(By the way, the `internal` keyword is *not* the same thing, it allows *all* classes within the entire assembly to access `internal` members, while `friend` allows you to give a certain class *complete access* to *exactly one* other class)


[1]:

[To see links please register here]

Reply

#2
You can get close to C++ "friend" with the C# keyword ["internal"](

[To see links please register here]

).
Reply

#3
I suspect it has something to do with the C# compilation model -- building IL the JIT compiling that at runtime. i.e.: the same reason that C# generics are fundamentally different to C++ generics.
Reply

#4
You should be able to accomplish the same sorts of things that "friend" is used for in C++ by using interfaces in C#. It requires you to explicitly define which members are being passed between the two classes, which is extra work but may also make the code easier to understand.

If somebody has an example of a reasonable use of "friend" that cannot be simulated using interfaces, please share it! I'd like to better understand the differences between C++ and C#.
Reply

#5
If you are working with C++ and you find your self using friend keyword, it is a very strong indication, that you have a design issue, because why the heck a class needs to access the private members of other class??
Reply

#6
For info, another related-but-not-quite-the-same thing in .NET is `[InternalsVisibleTo]`, which lets an assembly designate another assembly (such as a unit test assembly) that (effectively) has "internal" access to types/members in the original assembly.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through