这是什么意思(泛型) [英] What does this mean(Generics)

查看:93
本文介绍了这是什么意思(泛型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


它说您需要注意的另一个限制是使用

运算符==和!=

仅在将提供给泛型类型

的类型的值与null进行比较时才允许。

也就是说,以下代码可以正常工作。 />
这里如果T是一个值类型,那么它总是被假定为非null,所以在

中,上面的

代码比较总是返回true;


问题是它们对于使用运营商的文字是什么意思

==和!=
$ b只有在将提供给通用类型的类型的值

与null进行比较时才允许$ b。


public bool Compare(T op1,T op2) )

{

if(op1!= null&& op2!= null)

{

返回true;

}

其他

{

返回false;

}

}


// Tony

解决方案

6月18日,1:17 * pm,Tony < johansson.anders ... @ telia.comwrote:


它说你需要注意的另一个限制是使用

operator ==和!=

仅在将提供给泛型类型

的类型的值与null进行比较时才允许。

即,以下代码有效。

这里如果T是一个值类型,那么它总是被假定为非null,所以在

以上

代码比较总是返回true;"


问题是它们对于使用运算符的文本是什么意思

==和$ =

仅在将提供给泛型类型

的类型的值与null进行比较时才允许。



这意味着如果T不受约束,你就不能这样做:


T foo = ...;

T bar = ...;


if(foo == bar)


如果你约束T到是一个引用类型(即使用where T:class)

然后将使用引用标识比较,你可以比较

两个值


如果约束T是从超载==和!=

的类型派生的,那么将使用这些重载。这是*不*多态 - 只有

重载编译器可以保证在编译时使用。对于

实例,如果你有一个约束T:IEnumerable< char>并使用

" string"那么==仍然是指参考身份,而不是

使用==重载的字符串。


Jon


我觉得这个例子的有趣之处在于,如果你将T

约束为?struct?然后你会得到一个编译错误。


那么为什么当T不受约束而且它可以是一个类? ?

or- ** struct **你没有得到编译错误?

6月18日,7:24 * am,Jon Skeet [C#MVP ]" < sk ... @ pobox.comwrote:


6月18日,1:17 * pm,Tony < johansson.anders ... @ telia.comwrote:


它说你需要注意的另一个限制是使用

operator ==和!=

仅在将提供给泛型类型

的类型的值与null进行比较时才允许。

即,以下代码有效。

这里如果T是一个值类型,那么它总是被假定为非null,所以在

以上

code Compare总是返回true;"


问题是他们对文字说使用运营商是什么意思

==和!=是

仅在将提供给泛型类型

的类型的值与null进行比较时才允许。



这意味着如果T不受约束,你就不能这样做:


T foo = ...;

T bar = ...;


if(foo == bar)


如果你约束T到是一个引用类型(即使用where T:class)

然后将使用引用标识比较,你可以比较

两个值


如果约束T是从超载==和!=

的类型派生的,那么将使用这些重载。这是*不*多态 - 只有

重载编译器可以保证在编译时使用。对于

实例,如果你有一个约束T:IEnumerable< char>并使用

" string"那么==仍然是指参考身份,而不是

使用==重载的字符串。


Jon


< qg ********** @ mailinator.comwrote:


我对此示例感兴趣如果你将T

约束为a = 3Fstruct = 3F那么你会得到一个编译错误。


那么为什么当T不受约束时并且它可以是a = 3Fclass = 3F = 3F

or- ** struct **你don = 3Ft得到编译错误?



当它不受限制且在执行时T是一个结构,那么

与null的比较总是失败 - 即使是默认(T)。 (那是一个方便的方式告诉执行时它是否*是一个结构,

其实。)


如果你明确地将它限制为一个结构,那么比较

是毫无意义的,因为你总是知道答案。


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com


Hello!

It says "Another limitation that you need to be aware of is that using the
operator == and != are
only permitted when comparing a value of a type supplied to a generic type
to null.
That is, the following code works.
Here if T is a value type then it is always assumed to be non-null, so in
the above
code Compare always return true;"

The question is what does they mean with the text saying "using the operator
== and != are
only permitted when comparing a value of a type supplied to a generic type
to null."

public bool Compare(T op1, T op2)
{
if (op1 != null && op2 != null)
{
return true;
}
else
{
return false;
}
}

//Tony

解决方案

On Jun 18, 1:17*pm, "Tony" <johansson.anders...@telia.comwrote:

It says "Another limitation that you need to be aware of is that using the
operator == and != are
only permitted when comparing a value of a type supplied to a generic type
to null.
That is, the following code works.
Here if T is a value type then it is always assumed to be non-null, so in
the above
code Compare always return true;"

The question is what does they mean with the text saying "using the operator
== and != are
only permitted when comparing a value of a type supplied to a generic type
to null."

It means that if T is unconstrained you can''t do:

T foo = ...;
T bar = ...;

if (foo == bar)

If you constrain T to be a reference type (i.e. use "where T : class")
then reference identity comparisons will be used and you can compare
two values

If you constrain T to be derived from a type which overloads == and !=
then those overloads will be used. This is *not* polymorphic - only
overloads the compiler can guarantee at compile-time are used. For
instance, if you had a constraint "T : IEnumerable<char>" and used
"string" then == would still mean reference identity, rather than
using the == overloaded for string.

Jon


What I find interesting about this example is that if you constrain T
to be a ?struct? then you will get a compile error.

So why is it that when T its unconstrained and it can be a ?class? ?
or- **struct** you don?t get a compile error?
On Jun 18, 7:24*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:

On Jun 18, 1:17*pm, "Tony" <johansson.anders...@telia.comwrote:

It says "Another limitation that you need to be aware of is that using the
operator == and != are
only permitted when comparing a value of a type supplied to a generic type
to null.
That is, the following code works.
Here if T is a value type then it is always assumed to be non-null, so in
the above
code Compare always return true;"

The question is what does they mean with the text saying "using the operator
== and != are
only permitted when comparing a value of a type supplied to a generic type
to null."


It means that if T is unconstrained you can''t do:

T foo = ...;
T bar = ...;

if (foo == bar)

If you constrain T to be a reference type (i.e. use "where T : class")
then reference identity comparisons will be used and you can compare
two values

If you constrain T to be derived from a type which overloads == and !=
then those overloads will be used. This is *not* polymorphic - only
overloads the compiler can guarantee at compile-time are used. For
instance, if you had a constraint "T : IEnumerable<char>" and used
"string" then == would still mean reference identity, rather than
using the == overloaded for string.

Jon


<qg**********@mailinator.comwrote:

What I find interesting about this example is that if you constrain T
to be a =3Fstruct=3F then you will get a compile error.

So why is it that when T its unconstrained and it can be a =3Fclass=3F =3F
or- **struct** you don=3Ft get a compile error?

When it''s unconstrained and at execution time T is a struct, then the
comparison with null always fails - even for default(T). (That''s a
handy way of telling at execution time whether or not it *is* a struct,
in fact.)

If you explicitly constrain it to being a struct, then the comparison
is pointless as you always know the answer.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


这篇关于这是什么意思(泛型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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