VARIABLES IN C#
Consider a sitution where you have to create a program that accepts two numbers from user and display multiplication of the number on console screen.Now, while reading the number provided by the user you need to first store these number somewhere in memory so that you can performs the multiplication on the numbers.You can store the numbers in the memory by using variables.
Let's Take an Example
using System;
namespace multiplication
{
class Program
{
static void Main(string[] args)
{
int Number1, Number2; //Declare variables
Console.WriteLine("please enter the Number1");
Number1 = Convert.ToInt32(Console.ReadLine()); //Storing First User value into First variable 'Number1'
Console.WriteLine("please enter the Number2");
Number2 = Convert.ToInt32(Console.ReadLine()); //Storing Second User value into Second variable 'Number2'
int Result; //Declare variable for Store Result
Result = Number1 * Number2;
Console.WriteLine("Multiplication of two Numbers:" + Result.ToString());
Console.ReadLine();
}
}
}
Code Explanation
- In the preceding Example,Two variable of the data type int is declared.The name of the variable is 'Number1, Number2'.
- A variable is a location in the memory that has a name contains the value.
Naming Variables in C#
- A variable name must begin with a letter or an underscore ('_'),which may be followed by sequences of letters,digits(0-9),Or Underscore('_').
- The first character in a variable name cannot be a digit.
- A variable name should not contain any embebbed spaces or symbols, such as ?!@#$%^&*()[]{},.:;|\/.However, an underscore can be used wherever a space is required, like High_score.