如何在一个类型支持equaility没有==操作符检测?类型、操作、如何在、equaility

2023-09-07 09:14:00 作者:那年冬天

那么的Int32没有一个==操作符,但这是有效的code

 布尔Y = 6 = = 5;
 

这是因为的Int32是原始类型集的一部分。它有效地汇集到CEQ在IL。

所以,给出的类型(与反思)怎么可能是类型支持CEQ当它没有==操作符是确定?

解决方案

该规范说(§7.10):

  

在predefined整数比较操作符是:

 布尔运算符==(INT X,int y)对;
布尔运算符==(UINT X,UINT Y);
布尔运算符==(长×长Y);
布尔运算符==(ULONG X,ULONG y)的;
 

     

在predefined浮点比较运算符是:

 布尔运算符==(浮X,浮动Y);
布尔运算符==(双X,双Y);
 
我的世界之C外篇1

     

在predefined小数比较运算符:

 布尔运算符==(十进制的x,小数Y);
布尔运算符=(十进制X,小数Y)!;
 

     

在predefined布尔相等运算符为:

 布尔运算符==(BOOL X,布尔Y);
布尔运算符=(BOOL X,布尔Y)!;
 

     

每个枚举类型都隐式提供pdefined以下$ P $   比较运算:

 布尔运算符==(E X,E Y);
布尔运算符=(E X,E Y)!;
 

注意 decimal.operator == 是正常的方法,而不是一个内置的操作。我不知道为什么它列出。

So Int32 does not have an == operator but this is valid code

bool y = 6 == 5;

This is because Int32 is part of the primitive set of types. It effectively compiles to CEQ in IL.

So given a Type (with reflection) how can it be determined that that type supports CEQ when it has no == operator?

解决方案

The spec says (§7.10):

The predefined integer comparison operators are:

bool operator ==(int x, int y);
bool operator ==(uint x, uint y);
bool operator ==(long x, long y);
bool operator ==(ulong x, ulong y);

The predefined floating-point comparison operators are:

bool operator ==(float x, float y);
bool operator ==(double x, double y);

The predefined decimal comparison operators are:

bool operator ==(decimal x, decimal y);
bool operator !=(decimal x, decimal y);

The predefined boolean equality operators are:

bool operator ==(bool x, bool y);
bool operator !=(bool x, bool y);

Every enumeration type implicitly provides the following predefined comparison operators:

bool operator ==(E x, E y);
bool operator !=(E x, E y);

Note that decimal.operator == is a normal method, not a built-in operator. I'm not sure why it's listed there.