08-12-2010
Variable: Variable is representing a temporary value, the value can be changed.
Syntax: To declare variable
<data type> <variable name> = <value>;
Eg: int a = 10;
string s = "Stahya";
Eg: for variable:
void main(---)
{
int a = 10;
string s = "Sathya";
Console.WriteLine(a);
Console.WriteLine(s);
Console.ReadLine();
}
Output: 10
Sathya
Eg: for case sensitive:
void main(---)
{
int a = 10;
Console.WriteLine(a);
Console.ReadLine();
}
Output: Output for above program is error because C#.Net is a case sensitive language.
Local variables: The variables which is declared within a function can be called aslocal variable.
Local variable should be initialized at the time of declaration otherwise compiler will generate an error.
Eg: for local variable:
void main(---)
{
int a;
Console.WriteLine(a);
Console.ReadLine();
}
Output: The above program will generate an error because 'a' is a local variable, local variables should be initialized at the time of declaration like below: int a = 10;
Eg: Print a variable value with user defined string:
void main(---)
{
int a = 10;
Console.WriteLine("a value is " +a);
Console.ReadLine();
}
Output: a value is 10
To write the comments we will use two forwarding slashes(//).
Eg: To print multiple user defined message with variable values:
void main(---)
{
int a = 10;
int b = 20;
string s = "Sathya";
Console.WriteLine("a value is " +a);
Console.WriteLine("b value is " +b);
Console.WriteLine("s value is " +s);
Console.ReadLine();
}
Output: a value is 10
b value is 20
s value is sathya
Eg: To accept input from the user:
void main(--)
{
Console.WriteLine("Enter your name");
string s = Console.ReadLine();
Console.WriteLine("Your name is " +s);
Console.ReadLine();
}
Output: Enter your name
Sathya
Your name is Sathya