Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0034 C# Operator '-' is ambiguous on operands of type 'long' and 'ulong'

Tags:

c#

I'm getting this error:

CS0034 C# Operator '-' is ambiguous on operands of type 'long' and 'ulong'

I've tried several ways, but I couldn't fix it.

Here my code:

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);
        }
    }
}
like image 723
nurman syah Avatar asked Jan 03 '23 00:01

nurman syah


2 Answers

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

timer.Value is probably of type long.

this.counterAtStart is of type ulong.

The compiler doesnt know if it should use the - operand for ulong or long. Explicit casting your long to ulong should help:

ulong value = (ulong)this.timer.Value - this.counterAtStart;
like image 145
Hyarus Avatar answered Jan 04 '23 14:01

Hyarus


- is documented not to work on long and ulong operands!

C# Language Specification Section 7.3.6.2 (See the bold part):

Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

If either operand is of type decimal, the other operand is converted to type decimal, or a binding-time error occurs if the other operand is of type float or double.

Otherwise, if either operand is of type double, the other operand is converted to type double.

Otherwise, if either operand is of type float, the other operand is converted to type float.

Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a binding-time error occurs if the other operand is of type sbyte, short, int, or long.

This means you will have to decide on which overload of the - operator to use. Either cast timer.Value to a ulong, or declare counterAtStart as a long.

like image 32
Sweeper Avatar answered Jan 04 '23 13:01

Sweeper