gpt4 book ai didi

c# - 覆盖嵌套内部类中的运算符

转载 作者:行者123 更新时间:2023-11-30 16:11:19 25 4
gpt4 key购买 nike

我需要覆盖 Equals可访问性为 GetHashcode 的嵌套类中的运算符(以及因此的 internal 方法) .

当我尝试这样做时,编译器提示我无法重写等于 - a public成员 - 带有 private方法。但我无法覆盖 public ,因为类本身是 internal , 和嵌套。

如何做到这一点?我怎样才能覆盖 EqualsGetHashcode在一个不是 public 的类中(事实上​​,是嵌套和 internal )?

最佳答案

I'm not sure why it works, though.

此行为在 C# 规范的第 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 internal, the accessibility domain of M is the intersection of the accessibility domain of T with the program text of P.

此规定要求对于具有内部可访问性的类,类型成员可以等效地标记为公共(public)或内部。

You can't have a member whose access level is higher than it's containing class?

这是准确的;规范的同一部分指出“成员的可访问域永远不会比声明成员的类型的可访问域更具包容性”。重要的是要认识到,虽然语言允许具有内部可访问性的类型的成员声明为公共(public)的,但它仍然具有内部可访问性(就像它被声明为内部一样)。

换句话说,在下面的代码段中,X 的可访问性在两行中是等效的:

internal class A {public void X() {}}
internal class A {internal void X() {}}

关于c# - 覆盖嵌套内部类中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com