[英]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). 如果你想减少内存,可以将salary
为uint
而不是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
ulong
为ulong
:
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.