The Role of System.Convert
To wrap up the topic of data type conversions, I’d like to point out the fact that the System name-
space defines a class named Convert that can also be used to widen or narrow data:
static void NarrowWithConvert()
{
byte myByte = 0;
int myInt = 200;
myByte = Convert.ToByte(myInt);
Console.WriteLine("Value of myByte: {0}", myByte);
}
One benefit of using System.Convert is that it provides a language-neutral manner to convert
between data types. However, given that C# provides an explicit conversion operator, using the
Convert type to do your data type conversions is usually nothing more than a matter of personal
preference.
Do'stlaringiz bilan baham: |