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

前端 未结 7 1163
既然无缘
既然无缘 2021-02-18 15:52

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

How do I use the \'internal\' keyword?

I have read that \'internal\' keyword is a replacement for \'fr

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-18 16:20

    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.

提交回复
热议问题