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:
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?
defined type to built-in data 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:
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 doesnt have any data loss or possibility of exception occurrence, then you can perform implicit casting using keywords implicit and operator. If the above example doesnt throw ArgumentException then same code can be modified to use implicit casting as shown below:
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:
Output of this code will be:
Values are 50, 50
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
|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|