friend class in c#

  • Thread starter Thread starter krishjaya
  • Start date Start date
K

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?
 
"krishjaya" <[email protected]> a écrit dans le message de (e-mail address removed)...

|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 said:
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
 
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,
 
Back
Top