Arithmetic operations in C#

Mar 26, 2021 00:00 ยท 174 words ยท 1 minute read

The article briefly describes C# arithmetic operations as well as ways to convert types

+ , - , * , / , % , ++ , --

Bitwise Operations ๐Ÿ”—

&(logical multiplication),
|(logical addition),
^(logical XOR),
~ (logical negation or inversion)

Assignment operations ๐Ÿ”—

+= , -= , *= , /= , %= , &= , |= , ^= , <<= , >>=

Conditional expressions ๐Ÿ”—

==,!=,<,>,<=,>=,|,^,&,||,&&,!

What types can be safely converted to ๐Ÿ”—

byte » short, ushort, int, uint, long, ulong, float, double, decimal
sbyte » short, int, long, float, double, decimal
short » int, long, float, double, decimal
ushort » int, uint, long, ulong, float, double, decimal
int » long, float, double, decimal
uint » long, ulong, float, double, decimal
long » float, double, decimal
ulong » float, double, decimal
float » double
char » ushort, int, uint, long, ulong, float, double, decimal

Conversion ๐Ÿ”—

int x=500;
long y=(int)x;
Console.WriteLine(y);

uint u =10;
float f =101;
uint.TryParse(f.ToString(),out u);
Console.WriteLine(u);
y++;
Console.WriteLine(
"16 - numeric representation of a number {0} - {1}"
,y , Convert.ToString(y,16));
Console.WriteLine(
Convert.ToUInt16(++f));