Explicit Conversion

There are three ways of performing an explicit conversion in C#
0) Brackets - returns an instance of the type you are requesting or an exception. Also known as prefix casting or type cast operator.
1) As - returns an instance of the type you are requesting or null.
2) System.Convert - returns an instance of the type you are requesting or an exception.


There are three ways of performing an explicit conversion in VB.NET
0) DirectCast
1) CType
2) TryCast - the equivalent of As



Brackets

If this conversion is not possible then an exception is thrown.
This requires a cast operator and is also known as Casting.


int myint = 10; 
short myshort1 = (short)myint;

This is an example of a narrowing conversion


For classes an explicit conversion is needed to convert a base class to a derived class.

public class Base {} 
public class Derived : Base {}
Derived D = (Derived)(new Base());


As

If this conversion is not possible a null value is returned.
This conversion method is frequently used alongside the Is Operator.


int myint = 10; 
short myshort2 = myint as short;

For classes an explicit conversion is needed to convert a base class to a derived class

public class Base {} 
public class Derived : Base {}
Derived D = (new Base()) as Derived();

If the object cannot be cast successfully then "Nothing" is returned as opposed to generating an exception.
If the conversion is not possible then an exception is not thrown and the value null is returned.
This is useful because instead of having to handle a possible exception, you need to only test the returned value against Nothing.
This only operates on reference types (classes and interfaces)
It requires an inheritance or implementation relationship between the two types.
This means that the one type must inherit from or implement the other


Performing an explicit cast is not the same as using the As operator

  • An explicit cast throws an exception if the variable is not of the requested type or in the inheritance tree. The as operator will return null

  • The as operator can only be aplied to reference type variables being converted to reference types.

  • The as operator cannot perform user-defined conversions (eg explicit or implicit conversions operators) A prefix cast can perform user-defined conversions.


Using the as operator is significantly faster than using a prefix cast.
So avoid prefix casts, especially in a loop
When using the as operator do a type check immediately afterwards


void UseAsOperator(Animal a) 
Dog d = a As Dog
if (d!=null)
{
}

If the desired conversion is narrowing, it could fail at runtime.


Excel.Workbooks oWorkbooks = Application.Workbooks; 
Excel.Workbook oWorkbook = oWorkbooks.get_Item(1);
Excel.Sheets oSheets = oWorkbook.Worksheets;
Excel.Worksheet oWorksheet = oSheets.get_Item(1) as Excel.Worksheet;

Explicit type conversion is often called <Casting>.
This type of conversion requires a cast operator
To perform a cast place the type you are casting to in brackets in front of the variable to be converted
Examples include converting from larger to smaller integral types and converting from a base class to a derived class


double value3 = 1234.5;
int value4;
value4 = (int)value3;



Explicit casting can cause a loss of precision and/or throw exceptions
Converting a decimal to an integral type will truncate the value
Converting a decimal to float will round to the nearest decimal value.
Converting a double to float will round to the nearest float value. If the double is too large or too small then the result will be infinity or zero
Converting a float to double will round to the nearest number after the 28th decimal place



FromTo
sbytebyte , ushort, uint, ulong, or char
byteSbyte or char
shortsbyte , byte, ushort, uint, ulong, or char
ushortsbyte , byte, short, or char
intsbyte , byte, short, ushort, uint, ulong, or char
uintsbyte , byte, short, ushort, int, or char
longsbyte , byte, short, ushort, int, uint, ulong, or char
ulongsbyte , byte, short, ushort, int, uint, long, or char
charsbyte , byte, or short
floatsbyte , byte, short, ushort, int, uint, long, ulong, char, or decimal
doublesbyte , byte, short, ushort, int, uint, long, ulong, char, float, or decimal
decimalsbyte , byte, short, ushort, int, uint, long, ulong, char, float, or double



System.Convert

If this conversion is not possible then an exception is thrown.
This class contains methods to convert from one base type to another.
This class provides conversion functions as static methods.
Although it is worth noting that the method names are different to the actual datatype names.

int myint = 10; 
short myshort2 = System.Convert.ToShort(myint);

string mynumber = "14";
int myint2 = System.Convert.ToInt32(mynumber);

 ToBase64CharArray
 ToBase64String
boolToBoolean
byteToByte
 ToChar
 ToDateTime
 ToDecimal
shortToInt16
intToInt32
longToInt64
sbyteToSByte
floatToSingle
 ToString
ushortToUInt16
uintToUInt32
ulongToUInt64




© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext