关于c#接口的问题 [英] question about c# interface

查看:66
本文介绍了关于c#接口的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我不明白的代码示例:

I have a code sample I don't understand:

class LinkedList<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
    }

    IEnumerator IEnumerable.GetEnumerator()
    {

    }
}





1. IEnumerable.GetEnumerator(),将接口放在方法名称前的用法是什么?



2.为什么IEnumerable.GetEnumerator()不能有修饰符,如果我将其更改为:

public IEnumerator IEnumerable.GetEnumerator(),编译器说:修饰符'public'对此项无效



1. IEnumerable.GetEnumerator(), what's the usage put the interface in front of the method name?

2. why IEnumerable.GetEnumerator() cannot have a modifier, if I change this to :
public IEnumerator IEnumerable.GetEnumerator(), the compiler says : "The modifier 'public' is not valid for this item"

推荐答案

接口位于名称前面,告诉编译器此类应该在该接口中实现方法。

C#界面(初学者) [ ^ ]



界面是与外界的合约它的成员没有访问修饰符。
An interface is put infront of the name to tell the compiler that this class should implement methods in that interface.
Interfaces in C# (For Beginners)[^]

An interface is a contract to the outside world and its members do not have access modifiers.


1。

接口的方法名称前面的接口名称告诉编译器它是一个显式的方法实现。因此,这个方法只能从这个界面的实例调用。



2。

这种声明,已经定义谁可以调用该方法,即任何引用该特定接口的变量,用于实现该接口的对象。
"1."
The interface's name in front of the interface's method name, tells the compiler it's an explicit method implementation. Thus, this method can only be called from an instance of this interface.

"2."
This kind of declaration, already defines who can call the method, i.e., any variable that's a reference to this specific interface, for an object of that implements that interface.


这篇关于关于c#接口的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆