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

标签 c#

我收到这个错误:

CS0034 C# Operator '-' is ambiguous on operands of type 'long' and '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);
        }
    }
}

最佳答案

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

timer.Value可能 long 类型。

this.counterAtStart 是 ulong 类型。

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

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

关于c# - CS0034 C# 运算符 '-' 在类型为 'long' 和 'ulong' 的操作数上不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51817350/

相关文章:

c# - 如果图表数据为空,如何显示消息?

c# - 用于 netTcp 的绑定(bind)的 maxConnections 的最大大小

c# - VLC 服务器与 .NET C# 的通信

c# - 只要我按下按钮,我怎样才能制作一个winform按钮 "do something"?

C# 套接字忽略我的 SendTimeout 值

c# - 如何使用 C# 和 SMO 终止与 SQL Server 数据库的所有连接?

c# - 可以将条件编译符号添加到 csproj.user 文件吗?

c# - 为十进制值分配字符串值

c# - 如何在 Java 中使用 .Net 程序集

c# - 如何使用 Helix.SharpDX 体积渲染可视化 3D 标量场?