C# 内部类的公共方法与内部方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/711361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 14:22:59  来源:igfitidea点击:

public vs. internal methods on an internal class

c#public-method

提问by ScottS

internal class Foo
{
  public void Fee()
  {
    Debug.WriteLine("Fee");
  }

  internal void Fi()
  {
    Debug.WriteLine("Fi");
  }
}

I'm thinking that Fee() and Fi() are equally accessible since the entire class is already internal. Am I overlooking something? Is there any reason to choose public or internal for the methods in a case like this?

我认为 Fee() 和 Fi() 是同样可访问的,因为整个类已经是内部的了。我是否忽略了什么?在这种情况下,是否有任何理由为方法选择 public 或 internal?

采纳答案by Reed Copsey

The internal class Foodeclaration will override the accessibility of the public void Fee()method, effectively making it internal.

internal class Foo声明将覆盖该public void Fee()方法的可访问性,有效地使其成为内部。

In this case, using internal vs. public on the methods will have the same effect. The only reason I would choose public methods vs. internal methods in a case like this would be to ease transitioning to a public class in a future version, should you choose to do so.

在这种情况下,在方法上使用 internal 与 public 将具有相同的效果。在这种情况下,我会选择公共方法与内部方法的唯一原因是,如果您选择这样做,在将来的版本中可以轻松转换到公共类。

回答by Jhonny D. Cano -Leftware-

According to the msdn documentationyour Foo class won't be accesible outside your assembly, so it doesn't make any difference to mark the methods as internal or public; it even doesn't make difference by using the Attribute InternalsVisibleTo

根据msdn 文档,您的 Foo 类不能在程序集之外访问,因此将方法标记为内部或公共没有任何区别;它甚至没有通过使用属性 InternalsVisibleTo

回答by eglasius

You are correct, both Fee and Fi will be equally accessible.

你是对的,Fee 和 Fi 都可以平等访问。

From the CSharp Language Specification 3.0, under 3.5.2:

来自 CSharp 语言规范 3.0,在 3.5.2 下:

The accessibility domain of a nested member M declared in a type T within a program P is defined as follows (noting that M itself may possibly be a type):

? If the declared accessibility of M is public, the accessibility domain of M is the accessibility domain of T.

在程序 P 中的类型 T 中声明的嵌套成员 M 的可访问域定义如下(注意 M 本身可能是一个类型):

? 如果 M 声明的可访问性是公共的,则 M 的可访问性域是 T 的可访问性域。

So, even if Fee is declared as public, it will be just as accessible as Foo (i.e. internal).

所以,即使 Fee 被声明为 public,它也会像 Foo 一样可访问(即内部)。

回答by Marc Gravell

Actually - there is a big difference if you are using reflection; in particular, Silverlight can get very upset if you try and access internal methods via reflection, even if you would have had access. I've seen occasions when I've had to make a method public to make the code work on Silverlight, even though it works on regular .NET.

实际上 - 如果您使用反射,则有很大的不同;特别是,如果您尝试通过反射访问内部方法,Silverlight 会变得非常不安,即使您本来可以访问。我见过我不得不公开一个方法以使代码在 Silverlight 上工作的情况,即使它在常规 .NET 上工作。

You might find the same with partial trust in regular .NET.

您可能会发现对常规 .NET 的部分信任也是如此。

回答by Marc Gravell

The only thing missing here in the answer is why would you do this?

答案中唯一缺少的是你为什么要这样做?

Some libraries have a lot of classes that are not meant for the consumer of the library to touch but they must inherit interfaces marked as public. For instance I have a library with a class that inherits the IComparer interface but it is only used internally and I don't want to clutter the public aspect of my library. If I mark the implemented Compare function as internal the compiler complains that I am not implmenting the interface IComparer.

一些库有很多类不供库的使用者接触,但它们必须继承标记为公共的接口。例如,我有一个类的库,该类继承了 IComparer 接口,但它仅在内部使用,我不想弄乱我的库的公共方面。如果我将实现的比较函数标记为内部,编译器会抱怨我没有实现接口 IComparer。

So how do I successfully implement the interface and at the same time prevent it from being accessible in the public aspect of my library? Mark the class as internal but the implemented function as public.

那么我如何成功实现接口并同时防止它在我的图书馆的公共方面被访问?将类标记为内部,但实现的功能为公共。

回答by gnikolaropoulos

I would go with only internal methods if a class is internal. in case you change your mind and make the class public you can just do a text replace and you are done.

如果一个类是内部的,我只会使用内部方法。如果您改变主意并公开课程,您只需进行文本替换即可完成。

回答by user1343819

It would make a difference when you want your internal class to implement an interface. The method which is an implementation of some interface, would have to be Public.

当您希望内部类实现接口时,这会有所不同。作为某个接口的实现的方法必须是公共的。