c# - return ulong in inline-if -


this question has answer here:

is there reason why can't use following code?

ulong test(int a, int b) {     return == b ? 0 : 1; } 

it shows me:

cannot implicitly convert type 'int' 'ulong'. explicit conversion exists (are missing cast?) 

the following work:

ulong test(int a, int b) {     return false ? 0 : 1; } 

i know how solve problem. want know reason.

thanks.

let's take @ resulting il code of second method:

il_0000:  nop il_0001:  ldc.i4.1 il_0002:  conv.i8 il_0003:  stloc.0 il_0004:  br.s       il_0006 il_0006:  ldloc.0 il_0007:  ret 

at il_0001 literal 1 pushed onto stack (so expression return false ? 0 : 1; evaluated @ compile-time , embedded il constant) , @ il_0002 literal converted int64.

from msdn:

a constant-expression (section 7.15) of type int can converted type sbyte, byte, short, ushort, uint, or ulong, provided value of constant-expression within range of destination type.

since 1 in range of ulong data type, such conversion succeed. compiler knows , therefore performs implicit conversion.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -