21

To this SO question: What is the C# equivalent of friend?, I would personally have answered "internal", just like Ja did among the answers! However, Jon Skeet says that there is no direct equivalence of VB Friend in C#. If Jon Skeet says so, I won't be the one telling otherwise! ;P

I'm wondering how can the keyword internal (C#) not be the equivalent of Friend (VBNET) when their respective definitions are:

Friend VBNET

The Friend (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed from within the same assembly, but not from outside the assembly. [...]

internal C#

Internal: Access is limited to the current assembly.

To my understanding, these definitions mean quite the same to me.

Then, respectively, when I'm coding in VB.NET, I use the Friend keyword to specify that a class or a property shall be accessible only within the assembly where it is declared. The same in C#, I use the internal keyword to specify the same.

  1. Am I doing something or anything wrong from this perspective?

  2. What are the refinements I don't get?

  3. Might someone please explain how or in what Friend and internal are not direct equivalences?

Thanks in advance for any of your answers!

4 Answers 4

33

I've said there's no direct equivalent of the C++ "friend" concept. That's not the same as the VB.NET Friend concept, which is indeed equivalent to internal in C#.

Context is important - don't assume that the same word means exactly the same thing in all languages... "static" is a classic example :)

3
  • I guess I got confused by the "friend" keyword. In particular after having read Ja's answer which is "internal", and being said that that was not correct. So, both are equivalent when talking .NET, right? What is this InternalsVisibleToAttribute thing? Commented May 7, 2010 at 18:20
  • 3
    @Will: Sorry, you're right - Friend in VB is indeed equivalent to internal in C#. It was not correct in that question, because the question wasn't about VB. Will edit my answer here though :) For the record, InternalsVisibleTo is about one assembly being allowed access to the internal members of another.
    – Jon Skeet
    Commented May 7, 2010 at 18:22
  • +1 Thanks Jon for your answer, and the others who commented too. Commented May 7, 2010 at 18:23
6

When comparing .NET languages, VB's friend equates to C#'s internal. Meaning, anything marked as such can only be accessed from within the same project/assembly. It can be combined with protected for greater control over visibility.

The InternalsVisibleTo attribute can be useful for testing purposes; despite the name, it applies to VB as much as it does to C#. It should be noted that VB did not support the use of InternalsVisibleTo until .NET 4.

2
  • 1
    +1 For mentioning that VBNET didn't support InternalsVisibleTo until .NET 4.0. Thanks for your answer! =) Commented May 10, 2010 at 18:44
  • -1 for the VB needing .NET 4 comment: I have a VS2k8 VB Project and VB Test Project with InternalsVisibleTo in the former linking to the latter that does not work without it. (There is the separate unrelated Publicize feature creating _Accessor types that makes this somewhat redundant but it works nonetheless.) The comment on the MSDN page says it doesn't work with VB.NET in VS2k5. The correct caveat it's not supported by VB.NET 8 -- VS2k5 -- and below. It is supported with .NET 2.0 by VB.NET 9 -- VS2k8 -- and above.
    – Mark Hurd
    Commented Aug 17, 2013 at 5:56
6

Jon's (original) answer makes it clear that he's referring to the C/C++ friend keyword, which grants private access to another class. There is no direct equivalent in C#, but there is a way to extend internal to another assembly, largely for testing.

As far as I understand it, VB.Net Friend is the same as C# internal.

(I wrote the above just as Jon added an answer here.)

2
2

There is a rough equivalent of the C++ friend keyword in managed code. Although it works at the assembly level, not the class level. You can use the [InternalsVisibleTo] attribute.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.