C# - Variables in higher level
In this article i will teach you how you can use variables in most of your program.
A variable can be compared to a storage room, and is essential for the programmer. In C#, a variable is declared like this:
;
An example could look like this:
string name;
That's the most basic version. Usually, you wish to assign a visibility to the variable, and perhaps assign a value to it at the same time. It can be done like this:
= ;
And with an example:
private string name = "John Doe";
The visibility part is explained elsewhere in this tutorial, so let's concentrate on the variable part. We will jump straight to an example of actually using a couple of them:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string firstName = "John";
string lastName = "Doe";
Console.WriteLine("Name: " + firstName + " " + lastName);
Console.WriteLine("Please enter a new first name:");
firstName = Console.ReadLine();
Console.WriteLine("New name: " + firstName + " " + lastName);
Console.ReadLine();
}
}
}
Okay, a lot of this has already been explained, so we will jump directly to the interesting part. First of all, we declare a couple of variables of the string type. A string simply contains text, as you can see, since we give them a value straight away. Next, we output a line of text to the console, where we use the two variables. The string is made up by using the + characters to "collect" the different parts.
Next, we urge the user to enter a new first name, and then we use the ReadLine() method to read the user input from the console and into the firstName variable. Once the user presses the Enter key, the new first name is assigned to the variable, and in the next line we output the name presentation again, to show the change. We have just used our first variable and the single most important feature of a variable: The ability to change its value at runtime.
Another interesting example is doing math. Here is one, based on a lot of the same code we have just used:
int number1, number2;
Console.WriteLine("Please enter a number:");
number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Thank you. One more:");
number2 = int.Parse(Console.ReadLine());
Console.WriteLine("Adding the two numbers: " + (number1 + number2));
Console.ReadLine();
Put this in our Main method, and try it out. The only new "trick" we use here, is the int.Parse() method. It simply reads a string and converts it into an integer. As you can see, this application makes no effort to validate the user input, and if you enter something which is not a number, an exception will be raised. More about those later.
-
How to Make Your Own Miracle? Express Simple Gratitude Attitude.
| By Bruno_Vigneault | in Motivational
The attitude of expressing simple gratitude to the Divine is an important step to make your own miracle happen, to ...
-
How to Use the Regular Expressions in ASP.NET
| By 5min | in Software
Learn how use Regular Expressions to specify Input patterns....
-
How to Remove Projects from Visual Studio
| By 5min | in General
Visual Studio Remove Projects. How to remove projects from Visual Studio...
-
Microsoft Expressions Web - Understanding the Limitations of Expression Web
| By 5min | in Software
Learn about some of the limitations of Expressions Web and some of the most advanced and complex options...
-
Microsoft Expression Design - Introduction to Microsoft Expression Design
| By 5min | in Software
Introduction to Microsoft Expression Design software and Video Series and topics covered in this tutorial...
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , is integer , check Is Zero , Get Que | By xxris | in Programming
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , i...
-
How to access and use a Window's command line | By MaxwellPayne | in Programming
Learn about the Window's command line in DOS and how to use it....
-
How to Learn to Program Your Computer | By dsj8760 | in Programming
This article is about learning to program a computer. It is a general article giving tips on how to learn about pro...
-
Jailbroken iPhones get RickRolled | By explorer | in Programming
First iPhone worm, attacks via SSH and does the classic rick roll gag on the user....
-
C# - Looping in statements | By Victorius | in Programming
We must learn how we can loop in statements. Now here we go....
-
C# - The switch statement | By Victorius | in Programming
Another important statement is the switch statement. Now we will learn it :)...
-
C# - The if statement | By Victorius | in Programming
Now we will learn the most important statement....
-
C# - Variable types | By Victorius | in Programming
I will teach you what kind of variables can you use in C#...
-
C# - Hello world explained | By Victorius | in Programming
I will show you what's the life after Hello Word :)...








No comments yet.