Variables in C# for Beginners - Part I

Variables are an essential tool to any program in any language, they store data inputted by you (the programmer) or the user, and can be accessed and used over and over throughout your code. Here you will learn how to store, call and change variables in C#.

Table of Contents

Variables in C#

It is worth noting that variables are case sensitive, therefore a variable named myName is very different from Myname and myname.

C# (said c-sharp) is a statically typed language (meaning that the type of variable is predetermined by you, thus known at compile time), this means that writing C# can take a little longer, but it also means that minor bugs are avoided and accounted for.

In this article, the main variable types that will be addressed are integers, floats, doubles, strings, and booleans.

Some Examples

In C#, variables are written in the following format:

(variable type) (name given to the variable) = (actual value you want to store); The components before the equal sign are called a declaration, and the data after the sign is the initialization.

Variable types in C# are represented by:

int //interger(whole number - positve or negative) float //Decimal number (again positive or negative), only up to 7 digits double //like a float but can hold up to 16 digits, thus most widly used to be on the safe side decimal // string //this will be treated as text, whether it holds numbers or letters bool //Booleans are represented by either true or false char //Can only take a single character and is surrounded by single quotes ''
Code language: C# (cs)

There is a possibility of using the keyword β€˜var’ to indicate that there will be a variable of some sort but that its type is not known, however, this is not good practice and should be avoided.

Here is an example of data stored in these data types:

int myNum = 25; //note the variable name chosen is in camel case, a standard practise in C# float myDecimal = 26.89; //also note that lines must end with a semicolon double myDouble = 29.9874; //most used for decimals decimal myLongDec = 29.23932098098909, //extra long decimals string myName = "Myriam"; //notice the double quoted around the string stored bool myBoolean = true; //can also hold false char myChar = 'A'; //as above for char var myName = "Myriam"; //This is not good practice, it is best to assign a variable type as seen above instead
Code language: C# (cs)

It is also possible to declare a variable but not assign it a value right away, for example:

int myNum; myNum = 25; //this can be added at any stage of code writing, as long as it is not after the print statement
Code language: C# (cs)

The above variables can then be called using Console.WriteLine( ) :

Console.WriteLine(myNum); //will print 25 Console.WriteLine(myDecimal); //will print 26.89 Console.WriteLine(myName); //will print Myriam
Code language: C# (cs)

Variables can be used for a plethora of operations, below, we are going to see how we can store a variable that has been inputted by a user:

using system; namespace Variables_In_Action { class Program { static void Main(string[] args) { Console.WriteLine("Where are you from?"); string country = Console.ReadLine(); Console.WriteLine("I have never been to" + country + ", I would like to visit some time!"); Console.ReadKey(); }
Code language: C# (cs)

In the example above, the variable country stores a user input, which in C# is represented by the command Console.ReadLine( ); essentially asking the program to read a line written by the user and save it to the variable country.

The program is then asked to write to the user some text, adding the user’s answer to the first question, which makes it seem as if the computer is interacting with the user.

NB: the Console.ReadKey( ); is used to prevent the console from closing after executing the code.

πŸ”₯ Learn JavaScript - Beginner Friendly!

πŸ‘‰Β JavaScript Array Filter – Made Easy
πŸ‘‰Β JavaScript Array Find – Made Easy
πŸ‘‰Β Difference between Let and Const
πŸ‘‰Β Sorting Numbers in JS!
πŸ‘‰Β Store User Input in a variable with JS!
πŸ‘‰Β JavaScript Spread Operator – Made Easy
πŸ‘‰Β JavaScript Slice (Arrays) – Made Easy
πŸ‘‰Β JavaScript Slice(Strings) – Made Easy
πŸ‘‰Β JavaScript Includes – Made Easy
πŸ‘‰ JavaScript forEach (Arrays) - Made Easy
πŸ‘‰ toLocaleDateString in JavaScript - For Humans
πŸ‘‰ JavaScript Fetch API - Made Easy

Conclusion

This is how to create variables in C#. I hope you were able to improve your understanding with the help of our article.

In part II, we will explore the different ways to convert and change different types of variables.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.