473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1353
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
4
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages:...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
3
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look...
2
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH...
2
by: vermarajeev | last post by:
Hi, I have a simple question to ask. I was just trying some sample program using friend function. The program goes like this #include<iostream> using namespace std; class B;
0
by: Sister Ray | last post by:
I'm trying to create a simple form that sends an email using my company's exchange server. I'm using the System.Net.Mail Namespace of the .net framework 2.0. I've googled everywhere and i think my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.