Home All Groups Group Topic Archive Search About
Author
28 Jul 2006 10:24 AM
krishjaya
I would like to know in c# if there is any way to have 'friend' class
just like
in C++. I know there is the 'internal' keyword, but this will allow
access to all
the classes of the assembly and I want to allow access only to a
specific class.

if No what is the reason?

Author
28 Jul 2006 10:42 AM
Joanna Carter [TeamB]
"krishjaya" <krishj***@hotmail.com> a écrit dans le message de news:
1154082256.933580.156***@s13g2000cwa.googlegroups.com...

|I would like to know in c# if there is any way to have 'friend' class
| just like
| in C++. I know there is the 'internal' keyword, but this will allow
| access to all
| the classes of the assembly and I want to allow access only to a
| specific class.

Not yet, but there is the...

[assembly: InternalsVisibleTo("MyFriendAssembly")]

....attribute.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Author
28 Jul 2006 2:06 PM
Carl Daniel [VC++ MVP]
Joanna Carter [TeamB] wrote:
Show quote
> "krishjaya" <krishj***@hotmail.com> a écrit dans le message de news:
> 1154082256.933580.156***@s13g2000cwa.googlegroups.com...
>
>> I would like to know in c# if there is any way to have 'friend' class
>> just like
>> in C++. I know there is the 'internal' keyword, but this will allow
>> access to all
>> the classes of the assembly and I want to allow access only to a
>> specific class.
>
> Not yet, but there is the...
>
> [assembly: InternalsVisibleTo("MyFriendAssembly")]
>
> ...attribute.

.... which is a rather large hammer - a much larger hole in the type system
than friend would create.  Still, if you put the class that needs a friend
into an assembly by itself, and put the class that will be it's friend into
an assembly by itself, you get nearly the effect of friend, but who'd want
to do that?

Practially speaking, you're better off putting the friends in the same
assembly and using internal and just live with the fact that all the other
classes in that assembly have more access than they need.

-cd
Author
1 Aug 2006 1:00 PM
Sandeep
Maybe I misunderstand,
but if you want only one class to access it you could create it as a private
class inside that class.



Hello krishjaya,

Show quote
> I would like to know in c# if there is any way to have 'friend' class
> just like
> in C++. I know there is the 'internal' keyword, but this will allow
> access to all
> the classes of the assembly and I want to allow access only to a
> specific class.
> if No what is the reason?
>

AddThis Social Bookmark Button