List of Conversion Keywords in C# (C Sharp)

In your code, you might have converted value of one data type to another. This is meant as Casting. Casting can either be explicit or implicit. Converting from certain data type to another can be automatically done and hence you can directly assign values of such data types to one another. This is Implicit Casting. But there are certain conversions which cannot directly happen and you have to explicitly cast them. This is known as Explicit Casting. Here is an example for implicit and explicit casting:

class sampleClass {
public static void Main() {
int intVar1 = 100;
double doubleVar1 = 21.4;
double doubleVar2 = intVar1; //implicit casting
int intVar2 = (int) doubleVar1; //explicit casting
Console.WriteLine(“Converted values are: {0},{1}”, doubleVar2, intVar2);
}
}

Output of this program will be:

Converted values are: 100, 21

Most of you will be already familiar with this concept. This conversion happens between data types. What if there is a provision to convert and cast the following?

• user defined type to built-in data type
• built-in data type to user defined type
• one user defined type to another user defined type

Surprised with these options? You can perform all these conversions using C# conversion keywords. There are three conversion keywords provided by C#. They are explicit, implicit and operator keywords.

When you perform a conversion between two user defined types or between user defined and built-in data type, if there is a possible data loss or chances for any exception occurrence then you have to perform the conversion through explicit casting with the help of explicit and operator keywords. Here is an example to demonstrate explicit casting:

class sampleClass {
int classVar;
public sampleClass(int data) {
if (data == 0) { throw new ArgumentException(); }
else { classVar = data;}
}
public static explicit operator sampleClass(int data) {
return new sampleClass(data);
}
}
class testClass {
public static void Main() {
try {
int data = 300;
sampleClass obj = (sampleClass)data;
}
catch (Exception e) { }
}
}

In this example, you are casting an int value directly as user-defined class called sampleClass. The sampleClass constructor shows that there is a possibility of ArgumentException to occur. Hence you have to perform explicit casting. For this conversion to happen successfully, you define a method with name as the class name and including keywords explicit and operator in the method declaration.

In this method, you just instantiate sampleClass and assign the data passed as its property value. When you cast an int to sampleClass object in Main() method of testClass, this method declared with explicit and operator keywords will be triggered. Note that you should always use the operator keyword when you are performing user-defined conversions be it explicit or implicit.

If your conversion doesn’t have any data loss or possibility of exception occurrence, then you can perform implicit casting using keywords implicit and operator. If the above example doesn’t throw ArgumentException then same code can be modified to use implicit casting as shown below:

class sampleClass {
int classVar;
public sampleClass(int data) { classVar = data; }
public static implicit operator sampleClass(int data) {
return new sampleClass(data);
}
public static implicit operator int(sampleClass obj) {
return obj.classVar;
}
}
class testClass {
public static void Main() {
//Conversion from User-Defined Type to Built-In Type
sampleClass obj = new sampleClass(100);
int data = obj;
//Conversion from Built-In Type to User-Defined Type
int data2 = 500;
sampleClass obj2 = data2;
}
}

In this example, you convert from user-defined type sampleClass to built-in type int as well as convert from built-in type int to user-defined type sampleClass. Since it is implicit casting, you need not cast the class name or built-in type name in brackets. Instead you can directly assign the class name to the built-in type and vice versa.

So far you have seen the usage of operator keyword along with implicit and explicit keywords to perform conversion. In addition to it, operator keyword can also be used to overload a built-in operator. Here is an example:

class sampleClass {
int var1, var2;
public sampleClass(int var1, int var2) {
this.var1 = var1;
this.var2 = var2;
}
public static sampleClass operator +(sampleClass obj1, sampleClass obj2) {
return new sampleClass(obj1.var1 + obj2.var2, obj1.var2+obj2.Var1);
}
}
class testClass {
static void Main() {
sampleClass obj1 = new sampleClass(10,20);
sampleClass obj2 = new sampleClass(30,40);
sampleClass obj3 = obj1 + obj2;
Console.WriteLine(“Values are {0}, {1}”, obj3.var1, obj3.var2);
}
}

Output of this code will be:

Values are 50, 50

|Developing .Net applications for multiple locales | List of Conversion Keywords in C# |Storage and transfer of data using Serialization in .NET |C# (C Sharp) Unified Type System: Reference Types |Using Reflection at Runtime in .NET Framework |What are Generics in C# |Working with Generic Collections in .NET Framework |Working with Graphics in .Net |Working with Isolated Storage in .NET|


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.