views:

2118

answers:

6

What is the equivalant of a 'friend' keyword in C Sharp?

How do I use the 'internal' keyword?

I have read that 'internal' keyword is a replacement for 'friend' in C#.

I am using a dll in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherited class any way I want. The problem is that most of the code in the parent class has protected methods. Will using a friend somehow make it possible to access or call these protected methods?

+3  A: 

I think you are looking for internal.

This page has a pretty comprehensive comparison of VB/C#.

As others have said, though, subclasses do have access to protected methods so you should already see them.

Michael Haren
+2  A: 

1) Internal is the C# equivelant of the VB.NET 'friend' keyword, as you have guessed (as opposed to a replacement)

2) Usage is as follows

internal void Function() {}
internal Class Classname() {}
internal int myInt;
internal int MyProperty { get; set; }

3) It, basically, is an access modifier that stipulates that the accessibility of the class / function / vairiable / property marked as internal is as if it were public to the Assembly it is compiled in, and private to any other assemblies

johnc
+1  A: 

Internal is the equivalent of friend. A protected method is only available within the same class or from an inheritor. If you're trying to expose protected methods from an inheritor, you can wrap them in public methods.

Jon B
A: 

Your subclass will be able to access the protected members of the class you inherit.

Are you looking to give access to these protected members to another class?

strager
+7  A: 
  1. You can use the keyword access modifier internal to declare a type or type member as accessible to code in the same assembly only.

  2. You can use the InternalsVisibleToAttribute class defined in System.Rutime.CompilerServices to declare a type as accessible to code in the same assembly or a specified assembly only.

You use the first as you use any other access modifier such as private. To wit:

internal class MyClass {
    ...
}

You use the second as follows:

[assembly:InternalsVisibleTo("MyFriendAssembly", PublicKey="...")]
internal class MyVisibleClass {
    ...
}

Both of these can rightly be considered the equivalent of friend in C#.

Methods that are protected are already available to derived classes.

Jason
A: 

If you create a class on the same project as the source code you've received, and you don't inherit the source code's classes, yet you want have access to those protected methods, make those methods internal.

But if you want to purely inherit the the class(be it on same project or different one), no need to change those protected to internal. You can merrily inherit the class and have access those protected methods at the same time.

That being said, there's a reason why some of the methods in the source code you've received has protected access on methods instead of internal/friend. Methods are marked as protected to allow 3rd party developers who inherits from those class and let them have an access on those methods(protected) even they are in binary form (i.e. DLL)

Michael Buen