接口没有任何成员 - 不好的做法? [英] Interface without any members - bad practice?

查看:96
本文介绍了接口没有任何成员 - 不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
什么是一个标记接口的目的是什么?

它是不好的做法,创建一个完全空白的接口,如:

Is it bad practice to create a completely empty interface such as:

public interface ISomething
{
}

有是我想比别人区别对待特定对象的情况下,但我不要求任何新的行为。

There is a case where I would like to treat certain objects differently than others, but I don't require any new behavior.

推荐答案

我个人认为,空接口是不是一件好事。

I personally think that empty interfaces are not a good thing.

接口是为了描述行为契约。一个空的接口并没有描述任何行为 - 你没有定义任何形式的合同在这里的

Interfaces are meant to describe behavioral contracts. An empty interface doesn't describe any behavior - you're not defining any form of contract here.

的接口设计准则特别说:

避免使用标记接口(不带任何成员的接口)。

Avoid using marker interfaces (interfaces with no members).

自定义属性提供了一种方式来标记类型。有关自定义属性的更多信息,请参见编写自定义属性。自定义属性是首选时,你可以推迟到代码执行检查的属性。如果您的方案需要编译时检查,你不能遵守这一准则。

Custom attributes provide a way to mark a type. For more information about custom attributes, see Writing Custom Attributes. Custom attributes are preferred when you can defer checking for the attribute until the code is executing. If your scenario requires compile-time checking, you cannot comply with this guideline.

这篇关于接口没有任何成员 - 不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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