Header Ads

C# Tutorial - 4. declare variables in Visual C#

declare variable in C# programming


Welcome to the forth lesson of my Visual C# Tutorial. In today's lesson, we are going to learn how to declare different types of variables and assign values to them. Before we write codes for declaring variables ourselves in Visual Studio, I would like to spend a few moments for you to grasp what variables are as far as C# programming is concerned.

Step 1. Understanding what variables are                                                   

I am pretty sure that at some point of your life or education, you have seen simple algebra problems as following.

Y = X - 3 and X = 10
This algebra problem is giving us very good clue as to what variables are in C# programming. Basically, variables refer to the X and Y that have values, numeric values in this case. To put it simply, you can just think of variables as a bucket in your computer's memory that is capable of containing a wide range of values such as string, integer and so forth. It can be illustrated as following.

declare variable in C# programming

Basically, we can assign values to variables, retrieve them later and replace them with other values if required. Then, as you can see, variable Y contains variable X which is 10. It means that variables can be read by others so that the value of variable Y is 7. In this example, these buckets are containing numeric values but we could also have different types of buckets (variables) to hold different values. 

Step 2. Let's write actual codes to declare variables with Visual C#           

Let's create a new project called "declaringVariables". Just in case you don't actually know how to create new projects in Visual Studio, Let's get back to the first lesson and take a look at the first part of the tutorial. So I am going to write a bunch of codes right between those curly braces of static in this new project.

declare variable in C# programming

I just want you to write the same code as following on your own since it will be good practice for you to get used to C# programming. 

Tips. If you run into some error messages as you write codes, you can get back to the second lesson where we talked about some possible errors and solutions in relation to the C# syntax. 

declare variable in C# programming
Let's go ahead and break down from the first line of code. When you write the code "int x;", basically you are asking the .net runtime to create enough spaces in your computer's memory to hold numeric values.

Tips. One of the jobs for .net runtime is to interact with computer's memory and hardware. So it makes sense for it to be responsible for allocating spaces in memory for variables.
Then, i want you to notice that the keyword is "int". This keyword "int" is basically saying that the variable x and y would contain integer values. This integer variable can contain numbers from negative two billion and something to positive two billion and something. 

Tips. In mathematical term, integer refers to the number that has no fractions or decimal places.

declare variable in C# programming
Now we have created two buckets in our computer's memory to hold integer values. Then, on the next two lines of code, I assigned actual values, 10 to the variable x, and  read the value of variable x and add negative three to assign the value of 7 to variable y. 

Tips. The equal sign ( = ) is an assignment operator. It is basically setting the integer variables called X and Y to the value of 10 and 7 respectively. 
Then, I passed the value of variable y to the method "WriteLine(y)" through its parameter so that the value "7" can be displayed on the screen. Let's make sure that this application works right by hitting the "start" button on the toolbar at the top.

declare variable in C# programming






Then, we are going to have the console window with the integer value of 7 on the screen.

declare variable in C# programming
Step 3. Declare String variable                                                                   

Let me first comment those lines of code we wrote out so that they are not going to be executed when you run the application. Notice that the commented-out section turns into different color. In this way, we could better focus on declaring string variables.  


declare variable in C# programming


Tips. when you want to comment multiple lines of code out, you can use this format: /* codes you want to comment out /*, and this format: // code for single line of code. 
Then, I am going to declare string variable called "name". As you may guess, string variable uses "string" keyword. More importantly, you must put double quotes around its values as following. Then, I put the value of name variable in the parameter of WriteLine() method so that it can be printed on the console window.

Tips. string variables can hold alphanumeric values which mean characters, numbers and the combination of both. 
declare variable in C# programming



 Up to now, we have created two types of variables, int and string. Actually you can have whatever you want for variables' names. One thing that I want to point out at the moment is that less code is always better when it comes to programming. So we are going to declare our variables into one line which took two lines in this case.

declare variable in C# programming
You can also minimize the lines of code for string variable 'name'

declare variable in C# programming

Lastly, we can also use "var" keyword to type less. Since C# programming was developed, there have been a lot of innovation to reduce the amount of code. The keyword "var" is one of the innovations.

Basically, when you declare a variable with the keyword "var", you asking the C# compiler to determine the type of this variable based on its values such as integers, texts or alphanumeric values. So you must initialize, or supply, values to var variable.


declare variable in C# programming




Tips. "var" variable can only be used at a method scope. It means that this type of variable can only be declared inside the curly braces of a method. 

No comments

Powered by Blogger.