faqts : Computers : Programming : Languages : C#

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

61 of 74 people (82%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

C#: Class: Friend: Can someone tell me what a friend class is in C#?

Sep 25th, 2003 16:21
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 25 September 2003 - 03:07 pm ------------------

C#: Class: Friend: Can someone tell me what a friend class is in C#?

---

Use the keyword 'internal'.

---

If you declare a variable, procedure or function inside that class as
'internal', then by definition only classes inside an assembly can have
access to it.

---
---

The coarsest level at which accessibility can be controlled is at the
class.

---

In most cases, the only valid modifiers for a class are 'public', which
means that everybody can see the class, and 'internal'.

---

'Internal' is a way of granting access to a wider set of classes
without granting access to everybody, and it is most often used when
writing helper classes that should be hidden from the ultimate user of
the class.

---

In the .NET Runtime world, 'internal' equates to allowing access to all
classes that are in the same assembly as this class.

---

Note: in the C++ world, such accessibility is usually granted by the
use of 'friends', which provide access to a specific class.

'Friend' provides greater granularity in specifying who can access a
class, but in practice, the access provided by 'internal' is usually
sufficient.

[book: source: A Programmer's introduction to C# - Eric Gunnerson - 
Chapter 7]

---
---

I would like to know 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.

The 'friend' access modifier does not exist in C#.
As you noted there is the 'internal' modifier.
This is the best you can do really.

[Internet: source: http://groups.google.com/groups?hl=en&lr=&ie=UTF-
8&threadm=%2362mXJ05CHA.1516%
40TK2MSFTNGP12.phx.gbl&rnum=1&prev=/groups%3Fq%3Dwhat%2Bis%2Ba%
2Bfriend%2Bclass%2Bin%2B%2522C%2523%2522%26hl%3Den%26lr%3D%26ie%3DUTF-
8%26selm%3D%252362mXJ05CHA.1516%2540TK2MSFTNGP12.phx.gbl%26rnum%3D1]

---
---

Internet: see also:

C#: Assemblies: What are assemblies?
http://www.faqts.com/knowledge_base/view.phtml/aid/24800/fid/791

C++: Class: Friend: Can someone tell me what a friend class is in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/16164/fid/163

Delphi: Class: Friend: Can someone tell me what friend class is in 
Object Pascal?
http://www.faqts.com/knowledge_base/view.phtml/aid/16168/fid/175

Java: Class: Friend: Can someone tell me what friend class is in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/16166/fid/165

----------------------------------------------------------------------