Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Casting and Converting Between Types

DZone's Guide to

Casting and Converting Between Types

Learn a few different ways to convert a number to a string in C#, plus casting, in this quick tutorial.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Recently I was asked how to convert a number to a string. Let’s look at a few ways of approaching this problem.

Most objects in c# have a method called ToString() which displays the string representation of that object. This is because of inheritance, all objects inherit from System.Object which defines ToString().

Int32 is a struct so it inherits from System.ValueType which also inherits from System.Object

so in code

int a = 9;
string b = a.ToString();

Now let’s look at the reverse. However the reverse runs the risk of throwing an error, let’s look at why.

string b= “9”;
string c =”a”;
string d = “two”;

All are valid strings but only one can be converted to a number. Use the TryParse method to convert to a number.

int.TryParse(“9”, out int e);

TryParse will not throw an exception if the conversion fails, if it succeeds variable e will contain the result. Note an earlier version of c# required you to define the out parameter before using it with TryParse.

int.Parse exists to do the same thing however it will throw exceptions if a conversion is not possible. The same is true if you use Convert.ToInt32(“two”);

Casting

Casting is a way to explicitly telling the compiler that a type is actually another type and you are aware data loss will occur.

double x = 4.5;
int y = (int)x;

However it is not possible to cast a string to a number format as a string can contain any character not just number characters.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
web dev ,tutorial ,c# ,programming

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}