473,236 Members | 1,443 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,236 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 1348
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.