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

时间:2018-08-13 07:36:00

标签: c#

我收到此错误:

  

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);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

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

timer.Value的类型可能是

this.counterAtStart是ulong类型。

编译器不知道是否应将-操作数用于ulong或long。 显式地将您的长处投向乌龙应该会有所帮助:

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

答案 1 :(得分:2)

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

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

  

预定义+的操作数发生二进制数值提升,   –,*,/,%,&,|,^,==,!=,>,<,> =和<=二进制运算符。二元   数值提升将两个操作数隐式转换为通用类型   对于非关系运算符,它也变为   操作的结果类型。二进制数值提升包括   按照出现的顺序应用以下规则:

     

如果一个操作数的类型为十进制,则另一个操作数将被转换   键入小数,否则如果另一个操作数发生绑定时间错误   的类型为float或double。

     

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

     

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

     

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

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

答案 2 :(得分:1)

您没有提到this.timer.Value的类型,但是正如我想的那样,您的代码后面还有long的类型。

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

只需替换

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

通过

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