DEV Community

Cover image for Type Conversion in C#
Matthew Honour
Matthew Honour

Posted on

Type Conversion in C#

C# is a language with many types such as ints, strings and too many others to fit in this sentence 😅 Sometimes while working in C# you'll find yourself needing to convert between these types so, this blog will tell you all you need to know about type conversion in C#.


There are three ways that types are converted in C#:

1. Implicit Type Conversion

Implicit type conversion occurs automatically by the compiler when you are converting between types where no data loss can occur. For example if you are converting an 'int' to a 'long', the C# compiler will do this automatically. This is because, the 'long' data type can hold values up to 9223372036854775807 which is much larger than the maximum value the 'int' data type can store (2147483647). Since there is no chance for data loss, the variables can be casted automatically as shown bellow:

// implicit type conversion as ints are smaller than longs.
int a = 1;
long b = a;
Enter fullscreen mode Exit fullscreen mode

2. Explicit Type Conversion

Explicit type conversion is used where data loss is possible. This type conversion is not automatically done by the compiler as it warns you that data loss may occur. Therefore you need to explicitly tell the compiler that you recognize that data loss may occur but you still want to go through with the conversion anyway. This process is also referred to as casting. An example of explicit type conversion is shown below:

// explicit type conversion with no data loss.
long c = 1;
int d = (int)c;

// explicit type conversion with data loss.
// overflow occurs as value of e is too large to be stored in variable f.
long e = 1000000000000000000;
int f = (int)e;
Enter fullscreen mode Exit fullscreen mode

3. Converting Between Non Compatible Types

Some types in C# cannot be implicitly or explicitly converted between one another. A popular example of this is converting a string to an int as shown bellow:

string s = "123";
int i = (int)s;

/* the following compiler error is shown:

Cannot convert type 'string' to 'int' [PlaygroundUI]csharp(CS0030)

*/
Enter fullscreen mode Exit fullscreen mode

In times like this where you need to convert from a string to another primitive type, you can instead use methods made available in the 'Convert' .NET Core library. You can also use methods available in the primitive type itself. Examples of using these methods are shown below:

// conversion using the Parse() method in the primitive types:

int i = int.Parse("123");

bool b = bool.Parse("true");

// conversion using the methods in the 'Convert' class:

int convertedInt = Convert.ToInt32("123");

bool convertedBool = Convert.ToBoolean("true");

// ... more methods available for all other primitive types.
Enter fullscreen mode Exit fullscreen mode

To Conclude...

I hope this blog helped you understand how type conversion works in C#. Thanks for reading! 😄

Top comments (0)