C# 接口命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681700/
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 13:13:27  来源:igfitidea点击:

Interface naming convention

c#interfacenaming-conventions

提问by Frederick The Fool

This is a subjective thing of course, but I don't see anything positive in prefixing interface names with an 'I'. To me, Thingis practically always more readable than IThing.

这当然是一个主观的事情,但我认为在接口名称前加上“I”没有任何积极意义。对我来说,Thing实际上总是比IThing.

My question is, why does this convention exist then? Sure, it makes it easier to tell interfaces from other types. But wouldn't that argument extend to retaining the Hungarian notation, which is now widely censured?

我的问题是,为什么会存在这个约定?当然,它可以更容易地将接口与其他类型区分开来。但是,这个论点不会延伸到保留匈牙利符号,现在被广泛谴责吗?

What's your argument for that awkward 'I'? Or, more importantly, what could be Microsoft's?

你对那个尴尬的“我”有什么论据?或者,更重要的是,什么是微软的?

回答by Jon B

The reason I do it is simple: because that's the convention. I'd rather just follow it than have all my code look different, making it harder to read and learn.

我这样做的原因很简单:因为这是惯例。我宁愿只是遵循它,也不愿让我的所有代码看起来都不同,这让阅读和学习变得更加困难。

回答by Otávio Décio

I think it is better than adding a "Impl" suffix on your concrete class. It is a single letter, and this convention is well established. Of course you are free to use any naming you wish.

我认为这比在具体类上添加“Impl”后缀要好。它是一个单一的字母,这个约定已经很好地建立了。当然,您可以随意使用任何名称。

回答by Otávio Décio

To separate interfaces from classes.

将接口与类分开。

Also (this is more of a personal observation than dictated from upon high), interfaces describe what a class does. The 'I' lends itself to this (I'm sure it is a construct in grammar which would be great to whip out right now); an interface that describes classes that validate would be "IValidate". One that describes matching behavior would be "IMatch".

此外(这更多是个人观察而不是自上而下的规定),接口描述了类的作用。“我”适用于此(我确定这是语法中的一个构造,现在很适合使用);描述验证类的接口将是“IValidate”。描述匹配行为的一种是“IMatch”。

回答by Marc Gravell

Well, one obvious consideration would be the (very common) IFooand Foopair (when abstracting Foo), but more generally it is often fundamental to know whether something is an interface vs class. Yes it is partly redundant, but IMO is is different from things like sCustomerName- here, the name itself (customerName) should be enough to understand the variable.

嗯,一个明显的考虑因素是(非常常见)IFooFoo对(抽象时Foo),但更一般地说,知道某物是接口还是类通常是基础。是的,它部分是多余的,但 IMO 与诸如此类的东西不同sCustomerName- 在这里,名称本身 ( customerName) 应该足以理解变量。

But with CustomerRepository- it that a class, or the abstract interface?

但是用CustomerRepository- 它是一个类,还是抽象接口?

Also: expectation; the fact is, right or wrong, that is what people expect. That is almostreason enough.

还有:期望;事实是,对与错,这就是人们所期望的。这几乎是足够的理由。

回答by lexx

The fact of the matter is that everyone understands it and part of writing better code is making it easy to read and understand.

事实是每个人都理解它,编写更好的代码的一部分是使其易于阅读和理解。

回答by topchef

There is nothing wrong with NOT using I convention for interfaces - just be consistent and make sure it works not just for you but for whole team (if there is one).

不将 I 约定用于接口并没有错——只要保持一致并确保它不仅适用于您,而且适用于整个团队(如果有的话)。

回答by Kurt Schelfthout

Because you usually have an IThing anda Thing. So instead of letting people come with their own "conventions" for this recurring situation, a uniform one-size-fits all convention was chosen. Echoing what others say, the de facto standardness is reason enough to use it.

因为你通常有一个 IThing一个 Thing。因此,与其让人们为这种反复出现的情况制定自己的“惯例”,不如选择一种统一的、一刀切的惯例。与其他人所说的相呼应,事实上的标准是足以使用它的理由。

回答by LukeH

Do prefix interface names with the letter I to indicate that the type is an interface.

在接口名称前加上字母 I 表示类型是接口。

The guideline doesn't explain whyyou should use the Iprefix, but the fact that this is now an established convention should be reason enough.

该指南没有解释为什么你应该使用I前缀,但现在这是一个既定的惯例这一事实应该是足够的理由。

What do you have to gain by dropping the Iprefix?

去掉I前缀有什么好处?

回答by Jamie Ide

It's just a convention that's intent is to prevent name collisions. C# does not allow me to have a class and an interface named Client, even if the file names are Client and IClient, respectively. I'm comfortable using the convention; if I had to offer a different convention I'd suggest using "Contract" as a suffix, e.g. ClientContract.

这只是一个旨在防止名称冲突的约定。C# 不允许我有一个类和一个名为 Client 的接口,即使文件名分别是 Client 和 IClient。我很习惯使用约定;如果我必须提供不同的约定,我建议使用“Contract”作为后缀,例如 ClientContract。

回答by T.E.D.

It looks Hungarianish to me. Hungarian is generally considered a menace in strongly-typed languages.

对我来说它看起来像匈牙利语。匈牙利语通常被认为是强类型语言的威胁。

Since C# is a Microsoft product and Hungarian notation was a Microsoft invention, I can see where C# might be susceptible to its influence.

由于 C# 是 Microsoft 的产品,而匈牙利表示法是 Microsoft 的发明,因此我可以看出 C# 在哪些方面可能会受到其影响。