Let’s convert that variable in C#
Tags: csharp, beginners, programming, learning
Published: 2023-05-09 21:47:46

**Learn C#: Conversions**

_**Table of Contents:**_

1. [Learn C#: Variables](https://dev.to/thesnowmanndev/learn-c-variables-34bf)
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](https://kylemartin0819.wordpress.com/2023/05/09/conversions-in-c/) to learn more in depth about conversions. 

```
int number = 123456789;
long biggerNumber = number;
```
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
```
On [my blog](https://kylemartin0819.wordpress.com/2023/05/09/conversions-in-c/) 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);
```

---

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