- Back to Home »
- C#.NET »
- C#.NET Variables
Posted by :
Sudhir Chekuri
Thursday, 31 December 2015
C#.NET Variables are used to store values of a specific datatype. We can assign values that fall within the range of datatype of the variable.
Memory for the variable is allocated in stack memory if it is of value type.
Memory for the variable is allocated in heap memory if it is of reference type.
The value in variables varies throughout the program.
Before assigning a value to the variable we have to declare it.
Assigning value to the variable when it is declared is known as initialization.
Memory for the variable is allocated in stack memory if it is of value type.
Memory for the variable is allocated in heap memory if it is of reference type.
The value in variables varies throughout the program.
Before assigning a value to the variable we have to declare it.
Assigning value to the variable when it is declared is known as initialization.
C#.NET variable declaration
int i;
i=10;
Here i is a variable of type int.
So, it can store value between -2,147,483,648 to 2,147,483,648.
In the second statement value 10 is stored in I.
We can also intialize the variable with some value when it is created like below.
int i;
i=10;
Here i is a variable of type int.
So, it can store value between -2,147,483,648 to 2,147,483,648.
In the second statement value 10 is stored in I.
We can also intialize the variable with some value when it is created like below.
Example program to create, initialize and assign value to a C# int variable
Program
using System;
namespace variable
{
class Program
{
static void Main(string[] args)
{
int i=10;
i = 20;
Console.Write(i);
Console.ReadKey();
}
}
}
Output
20
Explanation
In the above program i is a variable of int datatype. It is intialized with a value of 10. In the next statement i is assigned with a new value 20. So, the value in the variable is changed from 10 to 20. When we print the value in variable i using WriteLine the output is 20.
In the same way we can create any number of variables in a program with specific datatypes and based on the datatypes values should be stored in variables.
Example program to take input from user and print it using place holder syntax
Program
using System;
namespace variable
{
class Program
{
static void Main(string[] args)
{
string name;
Console.Write("Enter your name: ");
name
= Console.ReadLine();
Console.Write("Your name is: {0}",name);
Console.ReadKey();
}
}
}
Output
Enter your name: Sudhir
Your name is: Sudhir
Explanation
{0} is the place holder syntax which replaces that place in the string with the value present in the variable in runtime.
Program
using System;
namespace variable
{
class Program
{
static void Main(string[] args)
{
string fname, lname;
int age;
Console.Write("Enter your first name:
");
fname = Console.ReadLine();
Console.Write("Enter your last name: ");
lname = Console.ReadLine();
Console.Write("Enter your age: ");
age
=int.Parse(Console.ReadLine());
Console.Write("Your name is: {0} {1}, Your
Age is: {2}",fname,
lname, age);
Console.ReadKey();
}
}
}
Output
Enter your first name: Bill
Enter your last name: Gates
Enter your age: 45
Your name is: Bill Gates, Your Age is: 45
Explanation
Fname and lname variables of string type are used to store values taken from user and age of int type is used to store the integer value after converting it from string to int type.
{0} {1} and {2} are the place holders to display value in variables in the required format.
Program 13
using System;
namespace variable
{
class Program
{
static void Main(string[] args)
{
int i = 20; float f = 23.234f; char c = 'b'; string s = "def"; bool b = false;
Console.WriteLine(i+f);
Console.WriteLine(c+s);
Console.WriteLine(i+c);
Console.WriteLine(i+s);
Console.WriteLine(b);
Console.ReadKey();
}
}
}
Output
43.234
bdef
118
20def
false
_
Explanation
There are 4 variables i, f, c, s, b with datatypes int, float, char, string, bool respecitively.
int and float is of numeric datatypes so variables i and f stores numbers and there is no need of having quotes around the values.
f is of float type so it can store numeric value with decimal point but when f should be the suffix for the value which is assigned to f.
c variable is of char type and it is capable of storing only one character at a time whereas variable s can store multiple characters as it is of string datatype.
b variable is capable of storing either true or false value only.
i + f = 20 + 23.234 = 43.234
As i and f are numeric types + is used as addition arthimetic operator and they are added.
c + s= b + def = bdef
As c is char and s is string + is used as concatenation operator and they are concatenated.
i + c = 20 + b = 20 + 98 = 118
As i is integer and c is char the ascii value of c is added to i ie., ascii of b is 98 added to 20 gives output as 118.
i + s = 20 + def = 20def
As s is string it cannot be converted into ascii value so here + is used for concatenating values of i and s.
b = false
Value inside b is false and it is printed as it is.