Bug 24410 - [Unified] Unable to compare int with nuint using operators <, >, ==, etc.
Summary: [Unified] Unable to compare int with nuint using operators <, >, ==,...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-11-10 17:48 UTC by Christopher Blackman
Modified: 2014-11-11 11:13 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Christopher Blackman 2014-11-10 17:48:59 UTC
Hi everyone,

I'm receiving the following build error when trying to compare an int with an nuint, with the following error:

Operator `<' is ambiguous on operands of type `int' and `System.nuint' (CS0034) (ThreadedCoreData)

For example, using the code below the build error will occur:
int i = 0;
nuint n = 1;
if (i < n)

This is with Xamarin.iOS and Xamarin Studio, on OSX Mountain Lion.
Comment 1 Sebastien Pouliot 2014-11-10 22:08:46 UTC
Yes, but that's also what you would get if you compared an `int` to a `ulong` [1]. E.g.

	int i = 0;
	ulong n = 0;
	if (i < n) {

will give you:

> Test.cs(8,8): Error CS0019: Operator `<' cannot be applied to operands of type `int' and `ulong' (CS0019) (dontlink-dual)

[1] `nuint` is a `ulong` for 64bits builds (and the C# compiler is right to report it).

If you're looking for a solution (there's no question above) you either need to:

(a) cast (it's ugly and best avoided), e.g.

	int i = 0;
	nuint u = 0;
	if ((nuint) i < u) {

(b) don't mix signed and unsigned, e.g. using `uint` would work:

	uint i = 0;
	nuint u = 0;
	if (i < u) {

(c) sometime, but not in this case (var i =0 won't compile), using `var` can make it easier (as the type will adapt).
Comment 2 Christopher Blackman 2014-11-11 11:13:50 UTC
Hi Sebastian,

Thanks for the info. The reason I reported it is because the compiler says the operator is ambiguous instead of saying it's not allowed, so my first guess was that the operator overload is defined somewhere and should be working but is conflicting with a similar overload. 

But I see, comparisons between 'nuint' and 'int' aren't allowed by design. My guess is that the "ambiguous" error would be coming from operators in 'int' with the primitive C# types that 'nuint' is implicitly converted to. Maybe something could be done to have the error explicitly state that that operator can't be applied, if possible.

Thanks again, I'll go ahead and close this bug.