【问题标题】:CS0034 C# Operator '-' is ambiguous on operands of type 'long' and 'ulong'CS0034 C# 运算符“-”在“long”和“ulong”类型的操作数上不明确
【发布时间】:2018-08-13 15:48:27
【问题描述】:

我收到此错误:

CS0034 C# 运算符“-”在“long”和“ulong”类型的操作数上不明确

我尝试了几种方法,但我无法解决它。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespace IEI.Label
{
    public class ReferenceNumber
    {
        private string macAddress;
        private HiResolutionTimer timer;
        private ulong counterAtStart;
        private ulong epochTime;
        private Random random;
        private readonly static Lazy<ReferenceNumber> _instance;

        public static ReferenceNumber Instance
        {
            get
            {
                return ReferenceNumber._instance.Value;
            }
        }

        static ReferenceNumber()
        {
            ReferenceNumber._instance = new Lazy<ReferenceNumber>(() => new
            ReferenceNumber());
        }

        public ReferenceNumber()
        {
            this.random = new Random();
            this.macAddress = this.GetHashedMacAddress();
            this.timer = new HiResolutionTimer();
            this.counterAtStart = (ulong)this.timer.Value;
            DateTime now = DateTime.Now;
            this.epochTime = (ulong)((now.Ticks - 621355968000000000L) / (long)10);
        }

        private string ConvertBase10ToBase36(ulong number)
        {
            string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string str1 = "";
            ulong length = (ulong)str.Length;
            ulong num = number;
            do
            {
                int num1 = (int)(num % length);
                num /= length;
                string str2 = str.Substring(num1, 1);
                str1 = string.Concat(str2, str1);
            }
            while (num > (long)0);
            return str1;
        }

        private string GetHashedMacAddress()
        {
            string machineName = (from nic in
            (IEnumerable<NetworkInterface>)NetworkInterface.GetAllNetworkInterfaces()
                                  where nic.OperationalStatus == OperationalStatus.Up
                                  select nic.GetPhysicalAddress().ToString()).FirstOrDefault<string>();
            if (string.IsNullOrEmpty(machineName))
            {
                machineName = Environment.MachineName;
            }
            return this.Hash(machineName);
        }

        private string GetRandomBase36DigitsSafe(int numRandomCharacters)
        {
            byte[] numArray = new byte[8];
            this.random.NextBytes(numArray);
            string base36 = this.ConvertBase10ToBase36(BitConverter.ToUInt64(numArray, 0));
            base36 = (base36.Length <= numRandomCharacters ?
            base36.PadLeft(numRandomCharacters, '0') : base36.Substring(0,
            numRandomCharacters));
            return base36;
        }

        private string Hash(string input)
        {
            string base36;
            using (SHA1Managed sHA1Managed = new SHA1Managed())
            {
                byte[] numArray = sHA1Managed.ComputeHash(Encoding.UTF8.GetBytes(input));
                base36 = this.ConvertBase10ToBase36(BitConverter.ToUInt64(numArray, 0));
            }
            return base36;
        }

        public string NewId(int numTimestampCharacters = 10, int numMacAddressCharacters = 2, int numRandomCharacters = 4)
        {
            string str = this.macAddress;
            str = (str.Length <= numMacAddressCharacters ?
            str.PadLeft(numMacAddressCharacters, '0') : str.Substring(0,
            numMacAddressCharacters));
            string randomBase36DigitsSafe =
            this.GetRandomBase36DigitsSafe(numRandomCharacters);

            //Here I have got the problem

            ***ulong value = this.timer.Value - this.counterAtStart * **;
            ulong frequency = (ulong)((double)((float)(value * (long)1000000)) /
            (double)this.timer.Frequency);
            string base36 = this.ConvertBase10ToBase36(this.epochTime + frequency);
            base36 = (base36.Length <= numTimestampCharacters ?
            base36.PadLeft(numTimestampCharacters, '0') : base36.Substring(base36.Length
            - numTimestampCharacters, numTimestampCharacters));
            return string.Concat(base36, str, randomBase36DigitsSafe);
        }
    }
}

【问题讨论】:

  • 让它们都变长怎么样?
  • 也许读过minimal reproducible example 的想法,重点是“最小”。我认为应该有可能出现相同的错误,例如仅 10 行代码。这使我们为您提供帮助的难度提高了 30 倍。删除与问题无关的所有内容
  • 很多时候,编程中的错误信息与一行代码有关,其中提到了行号。这是有用的信息,特别是如果您在 SO 上对受影响的行添加评论。
  • 嗨,欢迎来到堆栈溢出。我可以请您阅读有关如何最好地在此论坛 (stackoverflow.com/help/how-to-ask) 上提出好问题的指南,并最大限度地提高将来获得帮助的机会吗?
  • 我试过了,但没用。错误信息会像这样出现; “错误 CS1503 参数 1:无法从 'ulong' 转换为 'long' IEI.Label”

标签: c#


【解决方案1】:
ulong value = this.timer.Value - this.counterAtStart;

timer.Value可能是长型。

this.counterAtStart 是 ulong 类型。

编译器不知道它是否应该对 ulong 或 long 使用 - 操作数。 将您的 long 显式转换为 ulong 应该会有所帮助:

ulong value = (ulong)this.timer.Value - this.counterAtStart;

【讨论】:

    【解决方案2】:

    -记录不适用于 longulong 操作数!

    C# 语言规范第 7.3.6.2 节(见粗体部分):

    预定义 + 的操作数发生二进制数字提升, –、*、/、%、&、|、^、==、!=、>、= 和

    如果任一操作数是十进制类型,则转换另一个操作数 键入十进制,或者如果另一个操作数发生绑定时错误 是浮点型或双精度型。

    否则,如果其中一个操作数是 double 类型,则另一个操作数是 转换为 double 类型。

    否则,如果任一操作数为浮点类型,则另一个操作数为 转换为 float 类型。

    否则,如果任一操作数为 ulong 类型,则另一个操作数为 转换为 ulong 类型,或者如果另一个发生绑定时错误 操作数的类型为 sbyte、short、int 或 long。

    这意味着您必须决定使用- 运算符的哪个重载。要么将timer.Value 转换为ulong,要么将counterAtStart 声明为long

    【讨论】:

      【解决方案3】:

      您没有提到 this.timer.Value 的类型,但我认为它在您的代码后面有 long 类型。

      implicit 没有用于在 ulonglong 之间进行计算的运算符。因此,您必须在进行计算之前将它们转换为相同的类型。

      只需替换

      ulong value = this.timer.Value - this.counterAtStart
      

      ulong value = (ulong)this.timer.Value - this.counterAtStart 
      

      【讨论】:

        猜你喜欢
        • 2019-08-27
        • 2014-05-27
        • 1970-01-01
        • 2023-04-07
        • 2023-02-10
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 2022-06-11
        相关资源
        最近更新 更多