带点符号的 C# 方法实现

标签 c# syntax

正在阅读 article我在方法名称中遇到了以下 C# 语法。

private class sortYearAscendingHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
       ...
   }
}

我理解 Compare 方法是 IComparer 接口(interface)的方法,但是来自 C++ 我不确定这个语法是什么意思。如果 Compare 是接口(interface)的一部分,我希望仅像 int Compare(...) 那样提及它。为什么我们必须指定类?

最佳答案

这是一个显式接口(interface)实现,当您从包含相似(相同签名)函数但每个接口(interface)需要不同实现的多个接口(interface)派生时,您可以使用它。

更多信息可以在 MSDN 上找到.

(来自链接页面的示例):

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period. For example:

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface. Both method implementations are separate, and neither is available directly on the class.

关于带点符号的 C# 方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24871375/

相关文章:

c# - Field "account"&"card"永远不会被赋值,默认为Null

c# - 如何在 Picturebox 上获得滚动条

java - 关于在 Java 中创建通用列表数组的错误

java - 带有以下双括号的构造函数调用是什么?

c# - 为什么 { } 初始化需要 Add 方法?

c# - 大多数人如何在 .NET Web 应用程序中编写后台任务?

c# - 在 gridview 中删除不起作用

c# - 使用mysql插入数据时出现语法错误

更改结构中的数据

Java语法问题