本文介绍了在C#3.0中向上推广泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道C#3.0是否最终支持通用上传。考虑

以下代码在C#2中有效:


string [] xs = {" this"," is,aa ;,测试};

object [] ys = xs;


现在,类似地,我希望以下内容也能正常工作:


IEnumerable< stringxs = new string [] {" this","是"," a"," test" };

IEnumerable< objectys = xs;


截至目前,这不起作用,因为C#无法执行转换。

实际上,很明显为什么这段代码不起作用:它需要创建一个新对象的

,这个操作通常不支持

泛型目前。


然而,实际上没有技术上的理由不允许这通常

(建议的解决方案会有编译器生成隐式转换

方法,遗憾的是,目前还不可能,因为当前版本的C#不允许制定所需的

约束)。


所以,长话短说,我的问题:这是否适用于C#3.0?如果它是
并非:这是否有明智的理由?我经常需要这种代码

,我必须使用的变通方法就是烦人的。

解决方案



< snip>



你能提供一个你需要的例子。


亲切的问候,

Allan Ebdrup




< snip>



为什么你需要ys成为IEnumerable< object>

如果是因为你想通过它方法(M)采用

IEnumerable< objectparameter如下:


public void M(IEnumerable< objectparameter){...}


您可以将M改为这样的通用方法:

public void M< T>(IEnumerable< Tparameter){...}


这样你就可以传递IEnumerable< stringand IEnumerable< objectto M


IEnumerable< stringxs = ...

IEnumerable< objectys = ...

M(xs); //这是有效的

M(ys); //这是有效的


你在调用M时不必指定T的原因是因为类型可以推断为
.

这种方式你也可以使用类型参数来实现一个方法

IEnumerable< IMyInterfacelike this


public void M< T>(IEnumerable< Tparameter )其中T:IMyInterface {...}


这对你有帮助吗?


亲切的问候,

Allan Ebdrup



Hello,

I was wondering if C# 3.0 finally supported generic upcasting. Consider the
following code which does work in C# 2:

string[] xs = {"this", "is", "a", "test"};
object[] ys = xs;

Now, analogously, I would expect the following to work as well:

IEnumerable<stringxs = new string[] { "this", "is", "a", "test" };
IEnumerable<objectys = xs;

As of now, this doesn''t work because C# is unable to perform the conversion.
Actually, it is obvious why this code doesn''t work: it would require the
creation of a new object, an operation which is not generally supported for
generics at the moment.

However, there is actually no technical reason not to allow this in general
(a proposed solution would have the compiler generate implicit conversion
methods, which, unfortunately, is not possible at the moment either because
the current version of C# does not allow to formulate the required
constraint).

So, long story short, my question: Does this work in C# 3.0? And if it
doesn''t: Is there a sensible reason for this? I need this kind of code
constantly and the workarounds I have to use are nothing short of annoying.

解决方案

<snip>

Could you provide an example where you need this.

Kind Regards,
Allan Ebdrup


<snip>

Why do you need ys to be IEnumerable<object>
If it''s because you want to pass it to a method (M) that takes an
IEnumerable<objectparameter like this:

public void M(IEnumerable<objectparameter){...}

You could chang M to generic method like this:

public void M<T>(IEnumerable<Tparameter) {...}

that way you can pass both IEnumerable<stringand IEnumerable<objectto M

IEnumerable<stringxs = ...
IEnumerable<objectys = ...
M(xs); //this is valid
M(ys); //this is valid

The reason you dont have to specify T when calling M is because the type can
be inferred.
This way you can also pecify a method with a parameter of the type
IEnumerable<IMyInterfacelike this

public void M<T>(IEnumerable<Tparameter) where T : IMyInterface {...}

Does this help any?

Kind Regards,
Allan Ebdrup



这篇关于在C#3.0中向上推广泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:32