简体   繁体   English

运算符'+'在'ulong'和'int'类型的操作数上是不明确的(C#)

[英]operator '+' is ambiguous on operands of type 'ulong' and 'int'(C#)

I'm trying to add a ulong(Uint64) to a normal int value ,it gives me the error in the title (I'm using visual studio 2015 community) I have also tried casting all of it into ulong but it still doesn't work here is some example code 我正在尝试将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());
}

whichever the operator is (/,+,*,-) it still gives the same error :/ I have searched all over the place and can't find anything that solves this I have checked over 7 pages on msdn and no, Nothing :( 无论运算符是什么(/,+,*, - ),它仍然会给出相同的错误:/我已经遍布整个地方找不到任何可以解决这个问题的东西我已经在msdn上查了7页而没有,没有: (

I just want to know how to add , subtract ,divide or multiply them , and thank you :) 我只是想知道如何加,减,除或乘它们,谢谢:)

The problem is that you have casted the outcome of balance + salary instead of casting salary to ulong to match balance type. 问题是你已经投入了balance + salary的结果,而不是将salary投入到ulong以匹配balance类型。

This should suffice: 这应该足够了:

balance += (ulong)salary;

If you want to use up less memory, you can cast salary to uint instead of ulong (32-bit integer vs 64-bit integer). 如果你想减少内存,可以将salaryuint而不是ulong (32位整数与64位整数)。 Of course it does make sense in some really rare scenario (large amount of data and not so much memory). 当然,在一些非常罕见的情况下(大量数据而不是那么多内存)它确实有意义。 Also you need to take in account that casting signed integer to unsigned one can result in some (un)expected behaviour. 此外,您需要考虑将有符号整数转换为无符号整数可能会导致某些(非)预期行为。

You could cast int to ulong while adding: 您可以在添加时将int ulongulong

balance += (ulong)salary;

Or in the long form: 或者是长形式:

balance = balance + (ulong)salary;

The problem is that Ulong is always positive while an int is not. 问题是Ulong总是积极而int不是。 But its fine to cast salary as a uint as well and then add them. 但是,将薪水作为一个uint然后添加它们也可以。

This makes sense and gives no warnings. 这是有道理的,没有任何警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS0034 C#运算符&#39;-&#39;在类型&#39;long&#39;和&#39;ulong&#39;的操作数上不明确 - CS0034 C# Operator '-' is ambiguous on operands of type 'long' and 'ulong' 如何修复运算符&#39;==&#39;对于ulong和long类型的操作数不明确 - how to fix operator '==' is ambiguous on operands of type ulong and long 运算符“&”不能应用于“ ulong”和“ ulong *”类型的操作数 - Operator '&' cannot be applied to operands of type 'ulong' and 'ulong*' C#运算符&#39;+&#39;不能应用于类型&#39;IntPtr&#39;和&#39;int&#39;的操作数 - C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int' C#运算符&#39;/&#39;不能应用于&#39;方法组&#39;和&#39;int类型的操作数 - C# Operator '/' cannot be applied to operands of type 'method group' and 'int C#:operator&#39; - &#39;不能应用于&#39;string&#39;和&#39;int&#39;类型的操作数错误 - C#: operator '-' cannot be applied to operands of type 'string' and 'int' error 运算符“&lt;”不能应用于“long”和“ulong”类型的操作数 - Operator '<' cannot be applied to operands of type 'long' and 'ulong' CS0019 C# 运算符“||” 不能应用于“int”和“int”类型的操作数 - CS0019 C# Operator '||' cannot be applied to operands of type 'int' and 'int' 运算符“&gt;”不能应用于&#39;ulong&#39;和&#39;int&#39;类型 - Operator “>” cannot be applied to type 'ulong' and 'int' c#-Linq错误“运算符&#39;==&#39;无法应用于类型为&#39;int&#39;和&#39;System.Linq.IQueryable的操作数 <T> ”和RemoveRange - c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM