[英]CS0034 C# Operator '-' is ambiguous on operands of type 'long' and 'ulong'
[英]operator '+' is ambiguous on operands of type 'ulong' and 'int'(C#)
我正在嘗試將ulong(Uint64)添加到正常的int值,它給我標題中的錯誤(我正在使用visual studio 2015社區)我也嘗試將所有這些都投入到ulong但它仍然沒有這里的工作是一些示例代碼
ulong balance = 0;
int salary = 5;
private void button_click()
{
//this is what the error refrences:
balance = balance + salary;
//here's when I try to cast(gives the same error):
balance = (ulong)(balance + salary);
console.writeline("your balance is now : " + balance.tostring());
}
無論運算符是什么(/,+,*, - ),它仍然會給出相同的錯誤:/我已經遍布整個地方找不到任何可以解決這個問題的東西我已經在msdn上查了7頁而沒有,沒有: (
我只是想知道如何加,減,除或乘它們,謝謝:)
問題是你已經投入了balance + salary
的結果,而不是將salary
投入到ulong
以匹配balance
類型。
這應該足夠了:
balance += (ulong)salary;
如果你想減少內存,可以將salary
為uint
而不是ulong
(32位整數與64位整數)。 當然,在一些非常罕見的情況下(大量數據而不是那么多內存)它確實有意義。 此外,您需要考慮將有符號整數轉換為無符號整數可能會導致某些(非)預期行為。
您可以在添加時將int
ulong
為ulong
:
balance += (ulong)salary;
或者是長形式:
balance = balance + (ulong)salary;
問題是Ulong總是積極而int不是。 但是,將薪水作為一個uint然后添加它們也可以。
這是有道理的,沒有任何警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.