Converting - Explicit
Also known as Type Casting.
There are a number of different ways we can make an explicit data type conversion.
(Brackets)
This is a type of explicit conversion
If this conversion is not possible then an exception is thrown.
This requires a cast operator and is also known as Casting or Prefix Casting.
Integer to Long
int myInteger;
long myLong;
myInteger = 10;
myLong = (long)myInteger;
Console.WriteLine(myLong);
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());
Excel - Does this work
int myValue;
myValue = (int)cell.Left;
myValue = (float)cell.Left;
myValue = (double)cell.Left;
myValue = (single)cell.Left;
Worksheet.Shapes.addPicture(,,, System.Convert.ToSingle(Cell.Left,
System.Convert
This is a type of explicit conversion
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 data type names.
int myInteger = 10;
short myShort = System.Convert.ToShort(myInteger);
string myText = "14";
int myInteger = System.Convert.ToInt32(myText);
| FromBase64String | converts a byte array to a base64 encoded string | |
| FromBase64CharArray | converts a byte array to a base64 encoded character array | |
| ToBase64CharArray | ||
| ToBase64String | ||
| bool | ToBoolean | |
| byte | ToByte | |
| ToChar | ||
| ToDateTime | ||
| ToDecimal | ||
| short | ToInt16 | |
| int | ToInt32 | |
| long | ToInt64 | |
| sbyte | ToSByte | |
| float | ToSingle | |
| ToString | ||
| ushort | ToUInt16 | |
| uint | ToUInt32 | |
| ulong | ToUInt64 |
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
| From | To |
| sbyte | byte , ushort, uint, ulong, or char |
| byte | Sbyte or char |
| short | sbyte , byte, ushort, uint, ulong, or char |
| ushort | sbyte , byte, short, or char |
| int | sbyte , byte, short, ushort, uint, ulong, or char |
| uint | sbyte , byte, short, ushort, int, or char |
| long | sbyte , byte, short, ushort, int, uint, ulong, or char |
| ulong | sbyte , byte, short, ushort, int, uint, long, or char |
| char | sbyte , byte, or short |
| float | sbyte , byte, short, ushort, int, uint, long, ulong, char, or decimal |
| double | sbyte , byte, short, ushort, int, uint, long, ulong, char, float, or decimal |
| decimal | sbyte , byte, short, ushort, int, uint, long, ulong, char, float, or double |
Object.Parse()
assumes the data is valid and does its best to fit into the type
Can be used to convert a variable to a different type
System.Boolean.Parse("True")
System.Byte.Parse()
System.Char.Parse("A")
System.DateTime.Parse - link to specific page
System.Decimal.Parse
System.Double.Parse
System.Enum.Parse()
System.Int32.Parse("23")
System.Int64.Parse
System.Single.Parse
System.String.Parse
ParseExact
Only available with System.DateTime
Dates & Times > System.DateTimeParse
only allows the exact format and will throw an exception otherwise
TryCast
TryCast attempts to cast an object to a specific type and if it fails it just returns Nothing instead of an InvalidCastException error which needs to be handled
With TryCast you can test to be sure that the object is not Nothing before performing additional operations on it.
Dim objMailItem As Outlook.MailItem
objMailItem = TryCast(objItem, Outlook.MailItem)
TryParse
Introduced in .NET 2.0
It attempts to parse the value and if it is not successful it returns False
Instead of this
int mynumber;
try
{ mynumber = int.Parse("50"); }
catch (FormatException ex)
{ mynumber = 0; }
catch (OverflowException ex)
{ mynumber = 0; }
we can have
int mynumber;
if (int.TryParse("50",out mynumber) == false)
{ mynumber = 0; }
The our parameter automatically gets a default value but this is more explicit
TryParseExact
Only available with System.DateTime
Dates & Times > System.DateTimeParse
??
int myInt = System.Int32.Parse(mytext);
decimal myDecimal = System.Decimal.Parse(mytext);
Boxing
Unboxing
System.BitConverter
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext