泛型,约束和重载 [英] Generics, constraints and overloading

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

问题描述

我想提供两个具有相同名称的通用方法和

相同的参数,但具有不同的约束条件,例如:


void Explore< T>(T [] a,T [] b)其中T:struct {}

void探索< T>(T [] a,T [] b)其中T:class {}


值类型应使用第一种方法和引用类型

第二种方法。但是,这不会编译,因为C#编译器

报告已经有一个定义了相同名称的方法

和相同的参数。


约束似乎没有被考虑在内!?


还有其他一些处理价值和参考类型的方法

in不同的方法自动?我不能在C#中看到一个带有泛型的b
。但我可能忽视了一些事情。


Pierre

http://www.creativedocs.net/

解决方案



这两种情况的处理是否如此不同以至于您需要两种方法?

您可以使用Type.IsClass来查看它是否是一个类。你可以有一个if

的陈述,无论如何都要做你需要的。

-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" Pierre Arnaud" <运算** @ newsgroup.nospam>在消息中写道

新闻:%2 *************** @ TK2MSFTNGP15.phx.gbl ...

我'' d喜欢提供两个具有相同名称和相同参数的通用方法,但具有不同的约束条件,例如:

void Explore< T>(T [] a,T [] b)其中T:struct {}
void探索< T>(T [] a,T [] b)其中T:class {}

值类型应使用第一种方法和参考类型
第二种方法。但是,这不会编译,因为C#编译器报告已经有一个定义了相同名称的方法和相同的参数。

约束似乎不是考虑在内??

是否有其他方法可以自动处理不同方法的价值和参考类型?我不能在C#中看到一个带有泛型的东西。但我可能忽视了一些事情。

Pierre

http://www.creativedocs.net/



Ignacio Machin(.NET / C#MVP)写道:< blockquote class =post_quotes>

这两种情况的处理是如此不同以至于你需要两种方法吗?您可以使用Type.IsClass来查看它是否是一个类。你有
可以有一个if语句,无论如何都可以做你需要的。




这只是一个优化问题:


- 如果它是类类型,那么我必须做一些检查以确定

参数是否为空;我可以比较参考相等和需要

在调用object时是谨慎的.Equals(对象)


- 如果是结构类型,我不要不需要检查null和Equals可以

无需事后使用(甚至System.IEquatable< T> .Equals)


你看到了理念。如果我想在我的方法中进行一些空检查,那么我不能使用一个泛型类型,其中T未被指定为类

类型。如果我这样做,我就不能将值类型传递给我的方法...


Pierre


嗨皮埃尔,


Ignacio为我们提供了一种很好的方法来确定类型是否为类。

在您的方法中,您可以先使用IsClass来检查是否为T是一个参考类型

如下:


if(typeof(T).IsClass)

..... //供参考类型

其他

..... //价值类型


HTH。


Kevin Yu

=======

此帖子已提供按现状没有保证,也没有赋予

权利。


I''d like to provide two generic methods with the same name and
the same arguments, but with different constraints, such as:

void Explore<T>(T[] a, T[] b) where T : struct { }
void Explore<T>(T[] a, T[] b) where T : class { }

The value types should use the first method and the reference types
the second method. However, this won''t compile, as the C# compiler
reports that there is already a method defined with the same name
and the same arguments.

Constraints seem not to be taken in account !?

Is there some other means of handling value and reference types
in different methods automatically ? I can''t see one with generics
in C#. But I might have overlooked something.

Pierre

http://www.creativedocs.net/

解决方案

Hi,
Is the handling of those two cases so different that you need two methods?
You can use Type.IsClass to see if it''s a class or not. you can have an if
statement and do what you need in any case.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Pierre Arnaud" <op**@newsgroup.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...

I''d like to provide two generic methods with the same name and
the same arguments, but with different constraints, such as:

void Explore<T>(T[] a, T[] b) where T : struct { }
void Explore<T>(T[] a, T[] b) where T : class { }

The value types should use the first method and the reference types
the second method. However, this won''t compile, as the C# compiler
reports that there is already a method defined with the same name
and the same arguments.

Constraints seem not to be taken in account !?

Is there some other means of handling value and reference types
in different methods automatically ? I can''t see one with generics
in C#. But I might have overlooked something.

Pierre

http://www.creativedocs.net/



Ignacio Machin ( .NET/ C# MVP ) wrote:

Hi,
Is the handling of those two cases so different that you need two
methods? You can use Type.IsClass to see if it''s a class or not. you
can have an if statement and do what you need in any case.



It''s just an optimization issue:

- If it is a class type, then I have to do some checking to see if the
parameters are null; and I can compare for reference equality and need
to be prudent when calling object.Equals(object)

- If it is a struct type, I don''t need to check for null and Equals can
be used without afterthought (or even System.IEquatable<T>.Equals)

You see the idea. If I want to do some null checking in my method, I
can''t use a generic type for which T is not specified to be of class
type. And if I do, I can''t pass value types to my method...

Pierre


Hi Pierre,

Ignacio has provided us with a good way of idetifying if a type is a class.
In your method, you can first use IsClass to check if T is a reference type
like the following:

if(typeof(T).IsClass)
.....//for reference types
else
.....//for value types

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


这篇关于泛型,约束和重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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