20.2 Interface members

原创 2006年06月22日 03:13:00
The members of an interface are the members inherited from the base
interfaces and the members declared by the
interface itself.
interface-member-declarations:
interface-member-declaration
interface-member-declarations interface-member-declaration
interface-member-declaration:
interface-method-declaration
interface-property-declaration
interface-event-declaration
interface-indexer-declaration
An interface declaration may declare zero or more members. The members of
an interface must be methods,
properties, events, or indexers. An interface cannot contain constants,
fields, operators, instance constructors,
destructors, or types, nor can an interface contain static members of any
kind.
All interface members implicitly have public access. It is a compile-time
error for interface member declarations
to include any modifiers. In particular, interface members cannot be
declared with the modifiers abstract,
public, protected, internal, private, virtual, override, or static.
[Example: The example
public delegate void StringListEvent(IStringList sender);
public interface IStringList
{
void Add(string s);
int Count { get; }
event StringListEvent Changed;
Chapter 20 Interfaces
281
string this[int index] { get; set; }
}
declares an interface that contains one each of the possible kinds of
members: A method, a property, an event,
and an indexer. end example]
An interface-declaration creates a new declaration space (§10.3), and the
interface-member-declarations
immediately contained by the interface-declaration introduce new members
into this declaration space. The
following rules apply to interface-member-declarations:
? The name of a method must differ from the names of all properties and
events declared in the same interface.
In addition, the signature (§10.6) of a method must differ from the
signatures of all other methods declared in
the same interface, and two methods declared in the same interface may not
have signatures that differ solely
by ref and out.
? The name of a property or event must differ from the names of all other
members declared in the same
interface.
? The signature of an indexer must differ from the signatures of all other
indexers declared in the same
interface.
The inherited members of an interface are specifically not part of the
declaration space of the interface. Thus, an
interface is allowed to declare a member with the same name or signature as
an inherited member. When this
occurs, the derived interface member is said to hide the base interface
member. Hiding an inherited member is
not considered an error, but it does cause the compiler to issue a warning.
To suppress the warning, the
declaration of the derived interface member must include a new modifier to
indicate that the derived member is
intended to hide the base member. This topic is discussed further in §10.7.1
.2.
If a new modifier is included in a declaration that doesn?t hide an
inherited member, a warning is issued to that
effect. This warning is suppressed by removing the new modifier.
[Note: The members in class object are not, strictly speaking, members of
any interface (§20.2). However, the
members in class object are available via member lookup in any interface
type (§14.3). end note]
20.2.1 Interface methods
Interface methods are declared using interface-method-declarations:
interface-method-declaration:
attributesopt newopt return-type identifier ( formal-parameter-listopt ) ;
The attributes, return-type, identifier, and formal-parameter-list of an
interface method declaration have the same
meaning as those of a method declaration in a class (§17.5). An interface
method declaration is not permitted to


specify a method body, and the declaration therefore always ends with a
semicolon.
20.2.2 Interface properties
Interface properties are declared using interface-property-declarations:
interface-property-declaration:
attributesopt newopt type identifier { interface-accessors }
interface-accessors:
attributesopt get ;
attributesopt set ;
attributesopt get ; attributesopt set ;
attributesopt set ; attributesopt get ;
The attributes, type, and identifier of an interface property declaration
have the same meaning as those of a
property declaration in a class (§17.6).
The accessors of an interface property declaration correspond to the
accessors of a class property declaration
(§17.6.2), except that the accessor body must always be a semicolon. Thus,
the accessors simply indicate whether
the property is read-write, read-only, or write-only.
C# LANGUAGE SPECIFICATION
282
20.2.3 Interface events
Interface events are declared using interface-event-declarations:
interface-event-declaration:
attributesopt newopt event type identifier ;
The attributes, type, and identifier of an interface event declaration have
the same meaning as those of an event
declaration in a class (§17.7).
20.2.4 Interface indexers
Interface indexers are declared using interface-indexer-declarations:
interface-indexer-declaration:
attributesopt newopt type this [ formal-parameter-list ] {
interface-accessors }
The attributes, type, and formal-parameter-list of an interface indexer
declaration have the same meaning as
those of an indexer declaration in a class (§17.8).
The accessors of an interface indexer declaration correspond to the
accessors of a class indexer declaration
(§17.8), except that the accessor body must always be a semicolon. Thus,
the accessors simply indicate whether
the indexer is read-write, read-only, or write-only.
20.2.5 Interface member access
Interface members are accessed through member access (§14.5.4) and indexer
access (§14.5.6.2) expressions of
the form I.M and I[A], where I is an expression having an interface type, M
is a method, property, or event of
that interface type, and A is an indexer argument list.
For interfaces that are strictly single-inheritance (each interface in the
inheritance chain has exactly zero or one
direct base interface), the effects of the member lookup (§14.3), method
invocation (§14.5.5.1), and indexer
access (§14.5.6.2) rules are exactly the same as for classes and structs:
More derived members hide less derived
members with the same name or signature. However, for multiple-inheritance
interfaces, ambiguities can occur
when two or more unrelated base interfaces declare members with the same
name or signature. This section
shows several examples of such situations. In all cases, explicit casts can
be used to resolve the ambiguities.
[Example: In the example
interface IList
{
int Count { get; set; }
}
interface ICounter
{
void Count(int i);
}
interface IListCounter: IList, ICounter {}
class C
{
void Test(IListCounter x) {
x.Count(1); // Error
x.Count = 1; // Error
((IList)x).Count = 1; // Ok, invokes IList.Count.set
((ICounter)x).Count(1); // Ok, invokes ICounter.Count
}
}
the first two statements cause compile-time errors because the member
lookup (§14.3) of Count in
IListCounter is ambiguous. As illustrated by the example, the ambiguity is
resolved by casting x to the
appropriate base interface type. Such casts have no run-time costs?they
merely consist of viewing the instance
as a less derived type at compile-time. end example]
[Example: In the example
Chapter 20 Interfaces
283
interface IInteger
{
void Add(int i);
}
interface IDouble
{
void Add(double d);
}
interface INumber: IInteger, IDouble {}
class C
{
void Test(INumber n) {


n.Add(1); // Error, both Add methods are applicable
n.Add(1.0); // Ok, only IDouble.Add is applicable
((IInteger)n).Add(1); // Ok, only IInteger.Add is a candidate
((IDouble)n).Add(1); // Ok, only IDouble.Add is a candidate
}
}
the invocation n.Add(1) is ambiguous because a method invocation (§14.5.5.1)
requires all overloaded
candidate methods to be declared in the same type. However, the invocation
n.Add(1.0) is permitted because
only IDouble.Add is applicable. When explicit casts are inserted, there is
only one candidate method, and thus
no ambiguity. end example]
[Example: In the example
interface IBase
{
void F(int i);
}
interface ILeft: IBase
{
new void F(int i);
}
interface IRight: IBase
{
void G();
}
interface IDerived: ILeft, IRight {}
class A
{
void Test(IDerived d) {
d.F(1); // Invokes ILeft.F
((IBase)d).F(1); // Invokes IBase.F
((ILeft)d).F(1); // Invokes ILeft.F
((IRight)d).F(1); // Invokes IBase.F
}
}
the IBase.F member is hidden by the ILeft.F member. The invocation d.F(1)
thus selects ILeft.F, even
though IBase.F appears to not be hidden in the access path that leads
through IRight.
The intuitive rule for hiding in multiple-inheritance interfaces is simply
this: If a member is hidden in any access
path, it is hidden in all access paths. Because the access path from
IDerived to ILeft to IBase hides
IBase.F, the member is also hidden in the access path from IDerived to
IRight to IBase. end example]

提示"cannot instantiate abstract class due to following members"?

提示"cannot instantiate abstract class due to following members"?     解决方法:从基类派生来的类,其中有需要子类实现的纯虚函数必须实...
  • lxh1230119
  • lxh1230119
  • 2012年06月19日 08:56
  • 16118

提示"cannot instantiate abstract class due to following members"? .

提示"cannot instantiate abstract class due to following members"?     解决方法:从基类派生来的类,其中有需要子类实现的纯虚函数必须实...
  • lijun5635
  • lijun5635
  • 2013年12月11日 23:30
  • 1550

tomcat - 报错 No such device; No faulty members identified.

启动tomcat集群的时候出现以下报错,导致集群没办法启动: java.net.SocketException: No such device; No faulty members identifi...
  • u010229420
  • u010229420
  • 2016年07月24日 18:04
  • 1499

swift_028(Swift 的协议)

//***********swift学习之28--协议--*************************** /*  协议规定了用来实现某一特定功能所必需的方法和属性。  任意能...
  • shifang07
  • shifang07
  • 2016年10月31日 10:47
  • 218

[狗尾续貂第四篇]ActiveMQ集群

前言这一篇是看过lifetragedy的两篇系列的Apache ActiveMQ实战(2)-集群之后,所写。 主要是写过程中,遇到的问题,和简要的总结。关于ActiveMQ集群当我们的系统庞大之后,...
  • BuquTianya
  • BuquTianya
  • 2016年08月28日 16:18
  • 801

hdu 1250 高精度+斐波那契

Hat's Fibonacci                                  Time Limit: 2000/1000 MS (Java/Others)    Memory L...
  • yu_ming_cong
  • yu_ming_cong
  • 2015年08月14日 20:22
  • 533

Linux下编译安装Erlang和Elixir

Elixir介绍Elixir 是一个基于 Erlang 虚拟机的函数式、面向并行的通用编程语言。Elixir 以 Erlang 为基础,支持分布式、高容错、实时应用程序的开发,同时亦对其进行扩展使之借...
  • xiangxianghehe
  • xiangxianghehe
  • 2017年12月22日 10:08
  • 148

MongoDB安装及简单主从部署

安装系统Ubuntu14.04 apt-get install mongodb 配置文件/etc/mongodb.conf,可以修改dbpath(文档存储位置),bind_ip注释掉127.0.0...
  • Sherry_Rui
  • Sherry_Rui
  • 2016年07月21日 14:51
  • 782

scala 基本语法—4

scala 基本语法—4
  • hzdxw
  • hzdxw
  • 2016年07月12日 21:59
  • 621

MDX children与members的误区

在ssas中利用mdx进行查询时,children、members到底是不是一样的呢?可能有时你会看到它们查询出来的结果是一致的,或者是members查询出来的结果只比children查询的结果多一个...
  • bendanlzh
  • bendanlzh
  • 2009年07月01日 12:40
  • 2036
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:20.2 Interface members
举报原因:
原因补充:

(最多只允许输入30个字)