Learning C# — Variables, Numbers, and Text

Rutger McKenna
Analytics Vidhya
Published in
5 min readApr 17, 2020

If we are to continue our lessons with C#, we gotta understand more of the fundamentals to have a stronger grasp on the language. Just as with other languages, one of the most important thing to really understand how to do is to save variables, as well as work with our numeric/text data.

A Strong Typed Language

C# is a strong typed language, which means setting our variables is a little different than other languages. For C# to correctly identify what is being saved to our variable, we have to explicitly tell it what kind of data we’re saving. C# is also picky, which means its going to make sure that we’re programming in our strong typed datatypes before the program even runs. This is called being statically typed. This will stop the program from running if we accidentally try and subtract text from a number, or try and divide a sentence by a number!

C# Datatypes

While there are different types of data in all languages of programming, we have to be hyper aware in C#! There are some datatypes that are way more prominent than other types; types we use more often in other words. Regardless, its important to know them all! Here is a great chart to break down the different datatypes in C#.

Numeric Datatypes

int, which is short for integer, double, char, bool and string are definitely going to be our most commonly used datatype declarations. If you have used JavaScript before, you’ll know that to declare a variable we can say let or const before the variable name, then set it equal to what we want. Here we will use some of the words in the ‘type’ column from the chart above to declare what we want depending on what data we are storing.

string myName = “Rutger”;

Here is an example of me saving a string (or text data) to a variable titled ‘myName’ (notice how the variable name is in camel case). We will do the same for numbers with int or double, true or false with bool, and single letter strings with char. All of these are good to know and learn how to differentiate from each other. As that comes with time and practice, at least know this is how we declare in C#!

Converting Datatypes

Sometimes our saved variables will want to change datatypes; anything can happen in programming! There are ways to do this even though C# is strongly typed. Sometimes datatypes can easily be changed if it isn’t losing any data when doing so. This is called implicit conversion. To convert explicitly, a great and simple example is with int (integer or full number) and double (datatypes that aren’t as memory heavy as decimal) datatypes. We would do so like this:

double hereIsANumber = 4.22;

int nowItIsThisNumber = (int)hereIsANumber;

Always check to see if your conversion is needed to be explicit!

Text Datatypes

The main two text datatypes are strings and char datatypes. Making a string is quite simple; you just save the variable with quoted text. Remember that you can’t use quotes within your string unless you use single quotes to encapsulate the string!

string sayingHi = “Oh hi there!”;

string whatDidYouSay = ‘I think he said “hello” to me!’;

String Concatenation

String concatenation is a way for us to add strings together, which allows us to input variables or other pieces of saved data. This is great for interactivity, communicating with a user on their profile, or sending out mass emails with each receiver getting a ‘personalized’ greeting. To add strings together this is all one has to do in C#:

string favoriteSuperhero = “Batman”;

string favoriteCity = “Gotham”;

Console.WriteLine(“My favorite superhero is “ + favoriteSuperhero + “and they live in “ + favoriteCity + “.”);

Notice the necessary spaces taken to spread out the sentence from the added variables we inserted, and ended our Console.WriteLine with a period.

String Interpolation

String interpolation makes this all way simpler, and makes concatenation almost unnecessary. While it is a good thing to know, interpolation is really the golden standard to enter in a variable to a string. All we have to do is start our string with a ‘$’ sign and enter our variable between some curly braces.

string favoriteSuperhero = “Batman”;

string favoriteCity = “Gotham”;

Console.WriteLine($“My favorite superhero is {favoriteSuperhero} and they live in {favoriteCity}.”);

This looks a lot more natural, is quicker to write, and doesn’t leave as much of a margin of error with spacing and punctuation!

String Info and Manipulation

To get some simplistic information about our strings, we can ask about the size of the our strings or where a certain character is within our string. This is great when looking or authorizing a password! If we have a string saved to a variable, all we have to do is have myVariable.Length to find out our length! To grab a certain character, we can put myVariable.IndexOf(“a”) to find where the letter “a” appears. It’ll return a numeric value of where “a” exists in the string (and the index starts with 0, so if the word was apple, it’d return a 0 for the “a” index).

There are plenty of ways to manipulate a string as well, but a very simple way to do so is to make the entire string capitalized or lowercase. Since we are asking the program to do something, we have to make sure we’re instigating the function with parenthesis. To do so, we simply write myVariable.ToUpper() or myVariable.ToLower() to give us a string that is either capitalized or lowercased. Note! This gives us another string rather than transforming our original saved string. These functions come from the .NET library for C# and are built in!

Keep Exploring Possibilities

There are so many other ways to manipulate and transform your variables holding numeric or text information in C#. This is just an intro. Remember those datatypes in the chart above and research using that knowledge! Being able to create variables is the first major start into knowing how to work with C#, so retain this knowledge as a key base and keep researching!

--

--