expression with mixed types

T

Tony Johansson

Hello!

if I have this expression
long = int * uint * ushort * short;
how is the implicit conversion done here for the different types in this
expression.

//Tony
 
P

Peter Duniho

Tony said:
Hello!

if I have this expression
long = int * uint * ushort * short;

That's not legal C# syntax. Are you trying to refer to some arbitrary
expression where the operands have the given types?
how is the implicit conversion done here for the different types in this
expression.

See section 6.1, "Implicit conversions" in the C# specification for
details. The short answer is: there is no implicit conversion from
short to uint, which is the type the rest of the expression is
implicitly converted to, so you'll require a cast for the last operand.

There are implicit conversions for everything else, including the final
expression type of uint to long.

Pete
 
T

Tony Johansson

Peter Duniho said:
That's not legal C# syntax. Are you trying to refer to some arbitrary
expression where the operands have the given types?


See section 6.1, "Implicit conversions" in the C# specification for
details. The short answer is: there is no implicit conversion from short
to uint, which is the type the rest of the expression is implicitly
converted to, so you'll require a cast for the last operand.

There are implicit conversions for everything else, including the final
expression type of uint to long.

Pete

Hi!

I have tested this expression below and it works I don't get any compile
error or runtime error so the
implicit conversion work for me.

int int_ = 12;
uint uint_ = 13;
ushort ushort_ = 32;
short short_ = 45;
long l = int_ * uint_ * ushort_ * short_;

//Tony
 
P

Peter Duniho

Tony said:
I have tested this expression below and it works I don't get any compile
error or runtime error so the
implicit conversion work for me.

Okay. I guess the compiler is promoting all the operands to long, so of
course things work fine.

In any case, my previous reply stands. If you want to know the
specifics of implicit conversion in C#, read the specification.
 
A

Arne Vajhøj

if I have this expression
long = int * uint * ushort * short;
how is the implicit conversion done here for the different types in this
expression.

It gets evaluated as:

long = (((long)int * (long)uint) * (long)ushort) * (long)short;

1) you can look it up in the C# spec

section "14.2.6.2 Binary numeric promotions" says

• Otherwise, if either operand is of type long, the other operand is
converted to type long.
• Otherwise, if either operand is of type uint and the other operand is
of type sbyte, short, or int,
both operands are converted to type long.

2) you can test it

int v1 = 1;
uint v2 = 2;
Console.WriteLine((v1*v2).GetType().Name);

3) and what you *should* do is avoid it !

You should write code so that any programmer even without
C# knowledge can understand the calculation.

Try and use the same data types.

And if you need to convert then do it explicit. And be
generous with the use of parenthesis's.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top