- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
您可以在 sbyte 和 byte、int、uint、short、ushort、long、double 和 float 之间进行 >、<、== 等比较。但不是乌龙。
我的大脑在爆炸。谁能解释为什么 sbyte 可以与 uint 相比,但不是 ulong?
public bool sbyte_ulong_compare(sbyte x, ulong y)
{
return x < y; // compiler error CS0019
}
此外,使用 unchecked
不会让事情变得更好。大脑融化。
另一个编辑。这有效:
public bool sbyte_ulong_compare(sbyte x, ulong y)
{
//
// returns x < y
//
if (x < 0)
return true;
if (y > 127)
return true;
return ((long)x < (long)y);
}
最佳答案
dthorpe 和 Jon 的答案很接近但不太正确。
正确的推理如下。
规范指出:
For an operation of the form x op y, where op is a comparison operator, overload resolution is applied to select a specific operator implementation.
好的,重载解析必须使用哪些运算符实现?它们是:
bool operator <(int x, int y);
bool operator <(uint x, uint y);
bool operator <(long x, long y);
bool operator <(ulong x, ulong y);
bool operator <(float x, float y);
bool operator <(double x, double y);
bool operator <(decimal x, decimal y);
加上所有枚举类型的枚举小于运算符,加上上述每个类型的提升为可空版本。
过载解决方案必须首先消除不适用 运算符,然后从剩余的一组适用运算符中确定最佳 运算符。
int、uint、long 和 enum 运算符(及其提升形式)都被删除,因为 ulong 不会隐式转换为这些类型。
uint 和 ulong 运算符(及其提升形式)都被删除了,因为 sbyte 不会隐式转换为这些类型。
剩下的
bool operator <(float x, float y);
bool operator <(double x, double y);
bool operator <(decimal x, decimal y);
以及它们的提升形式。我们现在必须从这六个中确定最佳运算符。
我们所说的“最佳”是什么意思?比较两个运算符时,具有更具体 操作数类型的运算符更好。 “更具体”是指“Tiger”比“Animal”更具体,因为所有 Tigers 都可以转换为 Animal,但并非所有 Animals 都可以转换为 Tiger。
很明显,未提升的形式优于所有相应的提升形式。不可为 null 的类型比其对应的可为 null 的类型更具体,因为不可为 null 的类型始终可转换为其可为 null 的类型,但反之则不然。我们可以消除提升的形式。
还剩下三个。这三个哪个最好?
float 比 double 更具体。每个 float 都可以转换为 double,但不是每个 double 都可以转换为 float。因此消除了双重。剩下两个。
bool operator <(float x, float y);
bool operator <(decimal x, decimal y);
哪一个是最好的?没有从 float 到十进制的隐式转换。没有从十进制到 float 的隐式转换。因此两者都不比另一个好。
因此无法确定最佳运算符。重载解析失败。
我们决定报告一个通用的错误消息,简单地说没有这样的运算符可以做你想做的事,而不是给出看似奇怪和令人困惑的错误消息“运算符重载解析失败,因为 float 既不比 float 好也不坏十进制”。我认为这是一个合理的设计选择。
关于c# - 为什么我可以将 sbyte 与所有其他数字类型*except* ulong 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4327679/
如果我们在 ulong 表达式和 ulong? 表达式之间使用 == 运算符,则运算符重载 bool ulong(ulong left, ulong right) 似乎被使用。 换句话说,运算符认为两
我正在尝试将字典序列化为 JSON,并出现以下异常: new JavaScriptSerializer().Serialize(mydict)` Type 'System.Collections.Ge
我会将 ulong 值的 bin/oct/dec/hex 值作为字符串。所以我必须使用具有所需基数的 convert.tostring(, base) 。为了支持这一点,我将 ulong 值转换为 l
我有一个枚举,我想检查枚举类型是否为 ulong。 到目前为止尝试过: var checkValue = Enum.GetUnderlyingType(param.ParamType); // pa
我正在处理 std::bitset vector 中的数据,我都必须使用自制函数与 unsigned long(通过 std::bitset::to_ulong())和字符串相互转换(确切的算法与这个
我需要在 C# 中进行巨大的功率计算(想想 2 ^ 1,000,000),而 ulong 远远不够。 .NET 中可能没有任何更大的实现,但是是否有一些第 3 方的东西,或者甚至是我可以做的东西? 最
我目前正在尝试用 C# 制作一个应用程序,它需要一个 ulong number 输入和输出数字的口头表示(即 12 到“十二”,45238 到“四万五千二三十八”等),我希望它能够通过 uLong.M
我对 ulong 类型的行为有点困惑。我理解它是 64-bit integer used for positive numbers (最大值 18,446,744,073,709,551,615)。我
我有一段代码是 // Bernstein hash // http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
我正在尝试适应某些代码,这些代码会在某些数据的头部创建一个 char *。然后能够一次递增该指针 unsigned long 以读取数据。 有人告诉我执行此操作的一个好方法是使用 ifstream 将
这个问题在这里已经有了答案: How to compare Lists in Unit Testing (7 个答案) 关闭 5 年前。 考虑函数: public static ulong[] pr
我正在为加泰罗尼亚语数字编写程序。所以这里是公式: 我决定使用公式的中间部分,因为其他部分对我的知识来说太抽象了(也许我在数学课上睡得太多了)。实际上,我的程序适用于 n = 0;、n = 5;、n
我如何将十六进制/乌龙数的重要部分作为字符串? 例如,如果我有 0x00000321321,我想得到“321321”。 如果我尝试这样的事情: ulong x = 0x0000023632763; s
我想从 64 位字符串中删除位(由无符号长整数表示)。我可以通过一系列掩码和移位操作来完成此操作,或者像下面的代码一样迭代每一位。是否有一些巧妙的位操作方法可以使此操作更快? public ulong
我有以下方法: public static T ExecuteScalar( string query, SqlConnection connection, params Sq
这很好用: Public Const test As ULong = 1 << 30 这不能很好地工作: Public Const test As ULong = 1 << 31 它会产生此错误: C
我正在开发一个“简单”的基数转换器,将基数为 10 的 ULong 转换为任何基数的 String。这里我使用 64 个字符。 Usecase 是缩短 ULong,无论如何都存储为 String。 P
我正在开发一个“简单”的基数转换器,将基数为 10 的 ULong 转换为任何基数的 String。这里我使用 64 个字符。 Usecase 是缩短 ULong,无论如何都存储为 String。 P
如何将表示无符号长整型的 Long/ULong 转换为带填充零的无符号十六进制字符串(16 位十六进制字符串)? 我正在寻找 Kotlin 或 Java 中的简单而简洁的解决方案。 最佳答案 val
下面的代码允许用户输入一个整数并将其转换为二进制。输入的数字是(1) 解析成 long 类型和(2)转化为乌龙型。 namespace ConsoleApplication1 { class Prog
我是一名优秀的程序员,十分优秀!