DEV Community

Cover image for Let’s convert that variable in C#
Kyle Martin
Kyle Martin

Posted on

Let’s convert that variable in C#

Learn C#: Conversions

Table of Contents:

  1. Learn C#: Variables
  2. This post

Introduction:

My name is Kyle, I am aiming to write small, short read articles on Dev.to to help people learn C#. This is the second post in many where I write a summarized post of my recent blog posts on my blog. Let's get into it!


Conversions in C#:
In the previous post I wrote about simple types (value types) in the C# language. For this post, I will be writing about converting variables in the C# language. Most importantly I will be explaining two conversions: Implicit Conversions and Explicit Conversions.


Implicit Conversions:
In C# you can convert simple types into other types without having to cast (I will get into this soon). For example, you can change a int variable into a long, float, double, or decimal. The conversion will be done automatically by the compiler when certain expectations are met. To find out more about the demands, read my blog post to learn more in depth about conversions.

int number = 123456789;
long biggerNumber = number;
Enter fullscreen mode Exit fullscreen mode

Essentially, a variable defined and assigned earlier in code can be converted to another variable type as long as those language specifications are met.


Explicit Conversions:
In C# you can convert variables by explicitly telling the compiler to convert the variable. Explicit Conversions is also known as casting. For example, a double can be explicitly converted to a byte, short, int, long, char, float, or decimal by defining the type of the assignment. With casting, it can result in precision loss. If the double value is 1337.12 and you cast, it into an int it will become 1337.

double x = 1337.12;
int y = (int)x; // Output: 1337
Enter fullscreen mode Exit fullscreen mode

On my blog I also dive deeper into explaining Explicit Conversions, like I said earlier - my goal is to create summarized or condensed guides on dev.


Convert Class
In C# the System namespace provides a class named Convert that contains many methods for converting objects or variables as well. For example, you can use Convert.ToBoolean() to convert a string variable into a bool.

string doorOpenText = "True";
bool doorOpen = Convert.ToBoolean(doorOpenText);
Enter fullscreen mode Exit fullscreen mode

Again, I am aiming to consistently write condensed dev posts to summarize my lengthier blog posts. I have recently created a blog to capture the knowledge I have gained over the past five years of learning computer science and programming. Currently, I am fixated on C#. I know Dev.to really isn't a website where there are a lot of C# developers or learners, and it is more Javascript / front-end centric. I still am hoping to help some learners.

If you are interested in reading a lengthier post. Check out my blog:
https://kylemartin0819.wordpress.com/2023/05/09/conversions-in-c/

Don't forget to leave an emoji interaction, comment, or follow! My profile also has links to my GitHub where I post small applications, programming challenges, or learnings from online courses or books. I plan on doing programming book reviews as well someday.

Thanks for reading!

Kyle

Top comments (0)