gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:15:56 28 4
gpt4 key购买 nike

我收到这个错误:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com