471,326 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Using friend with C#

I am wondering if anyone out there is trying to use friend with C#, simular
to the way C++ handles it. I wrote a blog about it and I wonder if someone
can give me some feedback.

http://mikeperetz.blogspot.com/2008/...uld-i-say.html

Jun 27 '08 #1
4 1282
On Thu, 29 May 2008 17:54:21 -0700, Mike Peretz <m_******@hotmail.com>
wrote:
I am wondering if anyone out there is trying to use friend with C#,
simular to the way C++ handles it.
No, no one ever asks this.

:)

Sorry, being facetious. Here:
http://groups.google.com/groups/sear...C%2B%2B+friend

Here's the most recent thread I know about:
http://groups.google.com/group/micro...f4fe691b6a6b22
I wrote a blog about it and I wonder if someone can give me some
feedback.

http://mikeperetz.blogspot.com/2008/...uld-i-say.html
Well, one thing I'd say is that I think you're being a little hard on the
class in the non-static example, in calling it a "bully". The nested
class doesn't actually have access to the base class except through
itself. Inasmuch as the derived class "owns" anything it inherits in the
base class, it seems reasonable.

The big problem is that it doesn't really address the "friend" thing.
That is, a C++ friend could modify the member of _any_ instance of that
class, not just specific derived versions of it.

The static example provides a nice illustration of how dangerous protected
static members can be. Want to change it? Just inherit the class, and
you're in! No other class can control you. :)

If you read the specific thread I mentioned above, you'll see that it
doesn't really deal with "friend" per se either. However, it does discuss
a design pattern that IMHO may be a more appropriate approach to
"friend". I never really liked the idea of "friend" anyway, and I have
successfully written LOTS of code over the years without using it. There
always seem to be different, better approaches to managing relationships
between classes.

Some will argue (strike that...some _have_ argued) that I'm just naïve and
never wrote anything complex enough to warrant the use of "friend" and
that if only I had more experience, all would be clear to me. So feel
free to take what I say with a grain of salt (as you should anyway).
Suffice to say, I find those kinds of arguments ill-informed and somewhat
insulting. :)

Pete
Jun 27 '08 #2
To grant member-level access to functions that are not members of a
class or to all functions in a separate class, C++ has a mechanism to
use friends.

However this violates the rules of basic Object Oriented world!!! So
C# does not allow friends. (To the best of my knowledge)

-Cnu

On May 30, 5:54*am, "Mike Peretz" <m_per...@hotmail.comwrote:
I am wondering if anyone out there is trying to use friend with C#, simular
to the way C++ handles it. I wrote a blog about it and I wonder if someone
can give me some feedback.

http://mikeperetz.blogspot.com/2008/...th-c-or-should...
Jun 27 '08 #3
On May 30, 6:32 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
To grant member-level access to functions that are not members of a
class or to all functions in a separate class, C++ has a mechanism to
use friends.

However this violates the rules of basic Object Oriented world!!! So
C# does not allow friends. (To the best of my knowledge)
It doesn't have friend access in the same way as C++, but there is the
concept of a "friend assembly" using InternalsVisibleToAttribute. That
allows one assembly to access the internal members (not private - just
internal) of another. I've found it useful in unit testing, but not
elsewhere.

Jon
Jun 27 '08 #4
Duggi wrote:
To grant member-level access to functions that are not members of a
class or to all functions in a separate class, C++ has a mechanism to
use friends.

However this violates the rules of basic Object Oriented world!!! So
C# does not allow friends. (To the best of my knowledge)
I tend to disagree.

Encapsulation is a core aspect of OO.

But it is not obvious to me that friend is violating that.

You can grant access to everyone via public or sub classes
via protected or in C# to the assembly via internal. In C++
you can grant access to a specific class or specific method.

That is a finer granularity of control than C#. But that is
not less encapsulation. It could be argued to be more
encapsulation.

As with so many C++ constructs then it require a wise man
to decide when to use them - misuse is rather easy. So I
find it understandable that C# omitted that feature. The
benefit/complexity ratio is too small.

As has already been mentioned then C# do have a feature with
the same concept: InternalsVisibleToAttribute.

Arne
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by 胡岳偉(Yueh-Wei Hu) | last post: by
reply views Thread by Yueh-Wei Hu | last post: by
4 posts views Thread by marco_segurini | last post: by
8 posts views Thread by Douglas | last post: by
3 posts views Thread by Rob | last post: by
2 posts views Thread by vermarajeev | last post: by
reply views Thread by Sister Ray | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.