泛型限制...... [英] Generics limitation...

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

问题描述

我尝试比较泛型类中通用值类型T的值为

如下:


公共类C< T>其中T:struct

{

私有T值1;

私有T值2;


C (T值1,T值2)

{

this.value1 = value1;

this.value2 = value2;

}


public void SomeMethod()

{

if(value1!= value2)

DoSomething();

}

}


我在编译时收到以下消息:


运算符''!=''不能应用于T和T类型的操作数


Ok ,我可以使用以下代码:


if(value1.Equals(value2)== false)

DoSomething();


但这样做,我正在失去仿制药的所有优势

==>在比较发生之前,value1和value2被装箱......


变量初始化已经是这种情况,我需要在它之前向对象施加一个值

可以回到T(参见:2005年12月29日发布的泛型
$。b $ b限制.Net):


value1 =(T) (对象)Int32.MinValue;


因为它在我班上使用过一次,我不在乎额外的

拳击/拆箱操作。


但是value1 vs value2比较发生在实际情况的循环中(我的意思是

在我的真实程序中):


for(Int32 i = value1.Length; i--!= 0; i ++)

{

if(value1 [i]!= value2 [i])

DoSomething();

}


问题是:仿制药在这些注意事项中真的有用吗?

解决方案

>我尝试将泛型值类型T的值与泛型类进行比较为

按照:


您应该阅读:


我尝试比较泛型类中通用值类型T的两个值为

如下:


;-)

Luc Vaillant写道:

我尝试比较通用类中通用值类型T的值,如下所示:

公共类C< T>其中T:struct
{私有T值1;
私有T值2;

C(T值1,T值2)
{
this.value1 = value1;
this.value2 = value2;
}
public void SomeMethod()
{
if(value1!= value2)
DoSomething();
}
}
我在编译时得到以下消息:

运营商! =''不能应用于''T''和''T''类型的操作数

好的,我可以使用以下代码:

if( value1.Equals(value2)== false)
DoSomething();

但是这样做,我正在失去泛型的所有优势
==>在比较发生之前,value1和value2被装箱了......

已经是变量初始化的情况,我需要在对象转换为T之前对Object施加一个值
(参见:2005年12月29日发布泛型
限制.Net):

value1 =(T)(对象)Int32.MinValue;

自它在我的课堂上使用了一次,我不在乎额外的装箱/拆箱操作。

但是,在实际情况下,value1与value2的比较发生在循环中(我的意思是/>在我的真实节目中):

for(Int32 i = value1.Length; i--!= 0; i ++)
{
if(value1 [i]! = value2 [i])
DoSomething();
}

问题是:仿制药在这些注意事项中真的有用吗?



Luc,

你如何失去仿制药的优势?泛型并没有从类型中提升

类型的运算符。换句话说,如果你重载==

运算符(或者在这种情况下是!=运算符),那么你的通用

类是不可见的。


为了解决这个问题,你必须创建一个接口,它将公开操作并执行它们(而不是使用==或!=)。 br />
或者,或者你在内部为类分配一个类型,它将根据类型处理

的操作。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard。 caspershouse.com


" Luc Vaillant" <路********* @ discussions.microsoft.com>在留言中写道

news:88 ********************************** @ microsof t.com ...

我尝试与泛型类中的泛型值类型T的值进行比较,如下所示:

公共类C< T>其中T:struct
{私有T值1;
私有T值2;

C(T值1,T值2)
{
this.value1 = value1;
this.value2 = value2;
}
public void SomeMethod()
{
if(value1!= value2)
DoSomething();
}
}
我在编译时得到以下消息:

运营商! =''不能应用于''T''和''T''类型的操作数

好的,我可以使用以下代码:

if( value1.Equals(value2)== false)
DoSomething();

但是这样做,我正在失去泛型的所有优势
==>在比较发生之前,value1和value2被装箱......

已经是变量初始化的情况,我需要在它被强制转换之前对Object施加一个
值到T(参见:2005年12月29日发表的Generics
对.Net的限制):

value1 =(T)(对象)Int32.MinValue;
<因为它在我班上曾经使用过一次,所以我并不关心额外的装箱/拆箱操作。

但是,在实际情况下,value1与value2的比较发生在循环中(我的意思是
在我的真实节目中):

for(Int32 i = value1.Length; i--!= 0; i ++)
{
if(value1 [i]!= value2 [i])
DoSomething();
}

问题是:仿制药在这些注意事项中真的有用吗?


" Luc Vaillant" <路********* @ discussions.microsoft.com> écritdansle

message de news: 88 ********************************** @ microsoft.com ...


|我尝试比较泛型类中通用值类型T的值为

|按照:

|

|公共等级C< T>其中T:struct

| {

|私人T值1;

|私人T值2;

|

| C(T值1,T值2)

| {

| this.value1 = value1;

| this.value2 = value2;

| }

|

| public void SomeMethod()

| {

| if(value1!= value2)

| DoSomething();

| }

| }

|

|我在编译时收到以下消息:

|

| 运算符''!=''不能应用于''T''和''T''类型的操作数


你不能指望T支持==或!=运算符,因为某些类型可能没有超过它们的
。如果你想比较

泛型类中的两个Ts,那么你需要使用Equals(...)


但是,如果你需要比较你的泛型类的两个实例然后你需要提供这样的运算符重载:


公共类C< T>其中T:struct

{

私有T值;


公共覆盖bool Equals(对象obj)

{

返回值.Equals(((C< T>)obj).value);

}


public override int GetHashCode()

{

返回值.GetHashCode();

}


public static bool operator ==(C< T> lhs,C< T> rhs)

{

return lhs.value.Equals(rhs.value);

}


public static bool operator!=(C< T> lhs,C< T> rhs)

{

返回! lhs.value.Equals(rhs.value);

}

}


乔安娜


-

Joanna Carter [TeamB]

顾问软件工程师


I try to compare to values of generic value type T in a generic class as
follow:

public class C<T> where T : struct
{
private T value1;
private T value2;

C(T value1, T value2)
{
this.value1 = value1;
this.value2 = value2;
}

public void SomeMethod()
{
if (value1 != value2)
DoSomething();
}
}

and I got the following message at compile time :

"Operator ''!='' cannot be applied to operands of type ''T'' and ''T''"

Ok, I can use the following code :

if (value1.Equals(value2) == false)
DoSomething();

but doing so, I''m loosing all the advantage of generics
==> value1 and value2 are boxed before comparison occurs...

It was already the case for variable initialisation, I need to cast a value
to Object before it can be cast back to T (see : 12/29/2005 post "Generics
limitation on .Net") :

value1 = (T)(Object)Int32.MinValue;

since it was used once in my class, I didn''t care about extra
boxing/unboxing operation.

But value1 vs value2 comparison occurs in a loop in real situation (I mean
in my real program) :

for (Int32 i = value1.Length; i-- != 0; i++)
{
if (value1[i] != value2[i])
DoSomething();
}

The question is : Does generics really usefull in such sitautions ?

解决方案

> I try to compare to values of generic value type T in a generic class as

follow:
You should read:

I try to compare two values of generic value type T in a generic class as
follow:

;-)
"Luc Vaillant" wrote:
I try to compare to values of generic value type T in a generic class as
follow:

public class C<T> where T : struct
{
private T value1;
private T value2;

C(T value1, T value2)
{
this.value1 = value1;
this.value2 = value2;
}

public void SomeMethod()
{
if (value1 != value2)
DoSomething();
}
}

and I got the following message at compile time :

"Operator ''!='' cannot be applied to operands of type ''T'' and ''T''"

Ok, I can use the following code :

if (value1.Equals(value2) == false)
DoSomething();

but doing so, I''m loosing all the advantage of generics
==> value1 and value2 are boxed before comparison occurs...

It was already the case for variable initialisation, I need to cast a value
to Object before it can be cast back to T (see : 12/29/2005 post "Generics
limitation on .Net") :

value1 = (T)(Object)Int32.MinValue;

since it was used once in my class, I didn''t care about extra
boxing/unboxing operation.

But value1 vs value2 comparison occurs in a loop in real situation (I mean
in my real program) :

for (Int32 i = value1.Length; i-- != 0; i++)
{
if (value1[i] != value2[i])
DoSomething();
}

The question is : Does generics really usefull in such sitautions ?



Luc,

How are you losing the advantage of generics? Generics doesn''t lift the
type operators from the type. In other words, if you overload the ==
operator (or != operator in this case), that is NOT visible to your generic
class.

In order to get around it, you will have to create an interface which
exposes the operations, and performs them (instead of using == or !=).
Either that, or you assign a type to the class internally that will handle
the operations based on the type.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:88**********************************@microsof t.com...

I try to compare to values of generic value type T in a generic class as
follow:

public class C<T> where T : struct
{
private T value1;
private T value2;

C(T value1, T value2)
{
this.value1 = value1;
this.value2 = value2;
}

public void SomeMethod()
{
if (value1 != value2)
DoSomething();
}
}

and I got the following message at compile time :

"Operator ''!='' cannot be applied to operands of type ''T'' and ''T''"

Ok, I can use the following code :

if (value1.Equals(value2) == false)
DoSomething();

but doing so, I''m loosing all the advantage of generics
==> value1 and value2 are boxed before comparison occurs...

It was already the case for variable initialisation, I need to cast a
value
to Object before it can be cast back to T (see : 12/29/2005 post "Generics
limitation on .Net") :

value1 = (T)(Object)Int32.MinValue;

since it was used once in my class, I didn''t care about extra
boxing/unboxing operation.

But value1 vs value2 comparison occurs in a loop in real situation (I mean
in my real program) :

for (Int32 i = value1.Length; i-- != 0; i++)
{
if (value1[i] != value2[i])
DoSomething();
}

The question is : Does generics really usefull in such sitautions ?



"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 88**********************************@microsoft.com...

|I try to compare to values of generic value type T in a generic class as
| follow:
|
| public class C<T> where T : struct
| {
| private T value1;
| private T value2;
|
| C(T value1, T value2)
| {
| this.value1 = value1;
| this.value2 = value2;
| }
|
| public void SomeMethod()
| {
| if (value1 != value2)
| DoSomething();
| }
| }
|
| and I got the following message at compile time :
|
| "Operator ''!='' cannot be applied to operands of type ''T'' and ''T''"

You cannot expect T to support the == or != operators because some types may
not have overloaded them. If you are wanting to compare two Ts in the
generic class then you need to use Equals(...)

However, if you need to compare two instances of your generic class then you
nee to supply operator overloads like this :

public class C<T> where T : struct
{
private T value;

public override bool Equals(object obj)
{
return value.Equals(((C<T>) obj).value);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

public static bool operator ==(C<T> lhs, C<T> rhs)
{
return lhs.value.Equals(rhs.value);
}

public static bool operator !=(C<T> lhs, C<T> rhs)
{
return ! lhs.value.Equals(rhs.value);
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


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

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