Seph Cordovano
Seph Cordovano

Reputation: 2054

What's the difference between a Real Literal and Type Casting in C#?

Trying to understand the difference between the two. For example:

if(value >= 100M){...}

or

if(value >= (decimal)100){...}

Also, what's the industry standard? I see a lot of each and just want to make sure I fully understand the difference and am within best practices.

Upvotes: 2

Views: 281

Answers (1)

deyu
deyu

Reputation: 329

From a conclusion, C# compiler generates same result from either code, so you can use whichever you like.

I compiled your code and decompiled with ILSpy:

Before compile

var value = 1m;
if (value >= 100M)
{
}

if (value >= (decimal)100)
{
}

//This is the most simple
if (value >= 100)
{
}

Compile and decompile

decimal one = decimal.One;
bool flag = one >= 100m;
if (flag)
{
}
bool flag2 = one >= 100m;
if (flag2)
{
}
bool flag3 = one >= 100m;
if (flag3)
{
}

Upvotes: 2

Related Questions