Explicit Conversions C#

As the name suggests, an explicit conversion occurs when you explicitly ask the compiler to convert a value from one data type to another. Because of this, they require extra code, and the format of this code may vary, depending on the exact conversion method. Before you look at any of this explicit conversion code, you should look at what happens if you don ’ t add any.
For example, the following modification to the code from the last section attempts to convert a short value into a byte. See the following example explicit casting

byte destinationVar;
short sourceVar = 7;
destinationVar = (byte)sourceVar;
Console.WriteLine("sourceVar val: {0}", sourceVar);
Console.WriteLine("destinationVar val: {0}", destinationVar);

You may also like...