Today, wind marks asked a question in the group: What are the differences between assigning values to the following three variables defining the decimal type?
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Decimal
{
Class Program
{
Static void Main (string [] args)
{
Decimal dRebate1 = new decimal (1 );
Decimal dRebate2 = Convert. ToDecimal (1 );
Decimal dRebate3 = 1 M;
Console. WriteLine ("dRebate1: {0} \ ndRebate2: {1} \ ndRebate3: {2 }",
DRebate1.ToString (), dRebate2.ToString (), dRebate3.ToString ());
Console. ReadLine ();
}
}
}
I don't think there is any big difference. It's just that the source of the initialization value is different, just like using chopsticks or spoons for meals. Let's talk about it now.
1. the new dRebate1 instance is initialized to the specified 32-bit signed integer value, because the C # editor knows that it is a value type, so the instance of this integer is allocated on the thread stack, the value is 1, which is used to initialize a value. Right-click the definition and you will see the metadata in the Assembly mscorlib. dll. It is found that 1 in new decimal (1) cannot be a string or other character type, but only the following types:
2. dRebate2 converts the specified 32-bit (or 16-bit) signed integer value to an equivalent decimal number, that is, the basic type of decimal, here, it converts other basic types to decimal types. Of course, most basic types can be converted through Convert. You can click here to view the types of conversion types.
3. dRebate3, this is even more obvious. in C #, M or m indicates that this value is the basic type of decimal. If this is defined, tell C # editor, this value is of the decimal type and will be used later. Of course, if you write a string type "123" followed by a M, it will certainly report an error. F5 does not pass, because the editor does not know what this is, even if "123" Dad is Li Gang. Similar to the decimal basic type M, float uses F, f, double, and D.
Finally, I used the language.
For more information about each definition of the IL keyword, see chapter 3 in. NET and Chapter 5 in CLR via C.
Summary: when we define the type, we need to look at the specific occasions, and don't worry about who is better and who is worse. If we use the right scenario, it will be 100% better.