通用接口中的重载 [英] Overloading in generic interfaces

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

问题描述

各位大家好!


这是我在这里的第一篇文章。

有谁能解释一下以下代码的行为?

< code>

interface IFoo< T,P>

{

void Bar(T t,P p) ; //(1)

void Bar(P p1,P p2); //(2)

}


类NastyFooImpl:IFoo< int,int>

{

void IFoo< int,int> .Bar(int t,int p)

{

Console.WriteLine(" void IFoo< int,int> .Bar (int t,int

p)");

}


public void Bar(int p1,int p2)

{

Console.WriteLine(" public void Bar(int p1,int p2)");

}

}


类程序

{

static void测试< Foo,T,P()其中Foo:IFoo< ; T,P>,new()

{

Foo foo = new Foo();

T t = default(T);

P p =默认(P);

foo.Bar(t,p);

foo.Bar(p,p);

}

static void Main(string [] args)

{

测试< NastyFooImpl,int,int> ;();

}

}

< / code>


输出I 我正在使用Visual Studio 2005:

< code>

void IFoo < int,int> .Bar(int t,int p)

public void Bar(int p1,int p2)

< / code>


当我交换行(1)和(2)时,输出行的顺序改为

well。

任何链接都很大赞赏,特别是指向C#

规格的链接。

Hello everyone!

It''s my first post here.
Could anyone please explain me the behaviour of the following code?
<code>
interface IFoo<T, P>
{
void Bar(T t, P p); // (1)
void Bar(P p1, P p2); // (2)
}

class NastyFooImpl : IFoo<int, int>
{
void IFoo<int, int>.Bar(int t, int p)
{
Console.WriteLine("void IFoo<int, int>.Bar(int t, int
p)");
}

public void Bar(int p1, int p2)
{
Console.WriteLine("public void Bar(int p1, int p2)");
}
}

class Program
{
static void Test<Foo, T, P() where Foo : IFoo<T, P>, new()
{
Foo foo = new Foo();
T t = default(T);
P p = default(P);
foo.Bar(t ,p);
foo.Bar(p, p);
}
static void Main(string[] args)
{
Test<NastyFooImpl, int, int>();
}
}
</code>

The output I''m getting using Visual Studio 2005:
<code>
void IFoo<int, int>.Bar(int t, int p)
public void Bar(int p1, int p2)
</code>

When I swap lines (1) and (2), the order of lines of output changes as
well.
Any links are greatly appreciated, especially links to C#
specification.

推荐答案

5月30日上午9:40, gleb.alex ... @ gmail.com写道:
On May 30, 9:40 am, gleb.alex...@gmail.com wrote:

这是我在这里的第一篇文章。

有谁可以请我解释一下以下代码的行为?
It''s my first post here.
Could anyone please explain me the behaviour of the following code?



< snip>


Yowser,这太可怕了!


我今晚会密切关注这个规格并看看它是否有帮助。


(正如你所指出的)行为的变化取决于

接口声明顺序表明一些非常讨厌的东西。感觉

喜欢它不应该编译,但我不想确切地说到哪里;)


这是否是一个问题在生产代码?如果是这样,无论是否正式合法或者编译器真的应该是什么,b / b
,我建议你试着重构一下以避免这种情况 -

你真的不想让那些看过旁边代码的人不得不担心它们会担心它的问题。


Jon

<snip>

Yowser, that''s horrible!

I''ll look at the spec closely tonight and see whether it helps.

The fact that (as you point out) the behaviour changes depending on
interface declaration order suggests something very nasty. It feels
like it shouldn''t compile, but I wouldn''t like to say exactly where ;)

Has this come up as an issue in production code? If so, regardless of
whether it''s officially legal or what the compiler should really be
doing, I''d suggest trying to refactor things to avoid the situation -
you really don''t want to have whoever reads the code next to have to
worry about it :)

Jon


我不确定您的期望?


重载是在编译时确定的,并且对于泛型注意,

重载在* generic *术语中是固定的,而不是针对特定类型;


因此,给定T t &安培; P p,Bar(t,p)上的唯一匹配是通过

IFoo< T,P> .Bar(t,p),以及Bar上的唯一匹配(p,p) )是通过

IFoo< T,P> .Bar(p1,p2);因为这是早期决定*所有* P,T,

这里根本没有任何歧义。但是,如果你更快地关闭

通用(到int,int),那么*将*是一个歧义:


IFoo< int,intfoo = new NastyFooImpl();

foo.Bar(5,6); //哪个可以打电话?


我相信规格特别提到了一个相关案例,但我不能找到b
$ b。它类似于:

class SomeType< T {

this [int index] {}

this [T key] {}

}

然后引用SomeType< intby一个int索引器。 IIRC指数

版本胜过关键版本,因为它首先检查非泛型的

成员。


但是Jon说:yowser!这纯粹是学术性的吗?


Marc

I''m not sure what you expect?

Overloading is determined at compile time, and for generics note that
the overloads are fixed in *generic* terms, not for specific types;

As such, given "T t" & "P p", the only match on Bar(t,p) is via
IFoo<T,P>.Bar(t, p), and the only match on Bar(p,p) is via
IFoo<T,P>.Bar(p1, p2); because this is decided early for *all* P, T,
there isn''t any ambiguity here at all. If, however, you close the
generic sooner (to int, int), then there *would* be an ambiguity:

IFoo<int, intfoo = new NastyFooImpl();
foo.Bar(5,6); // which to call?

I believe the spec mentions a related case in particular, but I can''t
find the reference. It was something like:
class SomeType<T{
this[int index] {}
this[T key] {}
}
then refererring to SomeType<intby an int indexer. IIRC the "index"
version wins over the "key" version because it checks non-generic
members first.

But as Jon said: yowser! Is this purely academic?

Marc


撤回;我误读了两行(交换时)改变了

的行为。看起来有点奇怪的是界面如何实现成员的界面......非常怪异!这看起来很像

很像边缘情况,但非常非常好奇。还有一个罕见的

场合我们谈论纯C#(而不是模式,CLR,

等);-p


Marc

Retraction; I misread the 2 lines that (when swapped) changed the
behaviour. It looks like some freakery in how the interface
implementation members are ticked off... very freaky! This looks very
much like an edge case, but very very curious. And one of the rare
occasions we get to talk about pure C# (rather than patterns, CLR,
etc) ;-p

Marc


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

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