简体   繁体   中英

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.

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)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM