as Operator

This is a type of explicit conversion
A down cast and evaluates to null rather than throwing an exception.
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

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


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