A variable is a location in the memory.
A variable has a name and contains a value.
A variable is associated with a data type.
A variable refers to the memory address. When you create variable, it creates holds space in the memory that is used for storing temporary data. As you know about c# data types, each data type has predefined size.
The following figure shows the process of storing user input in the memory by using variables.
A variable has a name and contains a value.
A variable is associated with a data type.
A variable refers to the memory address. When you create variable, it creates holds space in the memory that is used for storing temporary data. As you know about c# data types, each data type has predefined size.
Data Types in a
programming language describes that what type of data a variable can
hold.
CSharp is a strongly typed language, therefore every variable and object must have a declared type.
The CSharp type system contains three Type categories. They are Value Types, Reference Types and Pointer Types.
In CSharp it is possible to convert a value of one type into a value of another type.
The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.
CSharp is a strongly typed language, therefore every variable and object must have a declared type.
The CSharp type system contains three Type categories. They are Value Types, Reference Types and Pointer Types.
In CSharp it is possible to convert a value of one type into a value of another type.
The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.
When we declare a variable,
we have to tell the compiler about what type of the data the variable can hold
or which data type the variable belongs to.
C# provides two types of
data types: Value
types and Reference types.
Value Types:
Directly contain data. The following figure shows the memory
allocation of an int variable.
Example
int age=25;
int num=5;
Example
int age=25;
int num=5;
float fees=1500.00;
Data Types
|
Size
|
Values
|
sbyte
|
8 bit
|
-128 to 127
|
byte
|
8 bit
|
0 to 255
|
short
|
16 bit
|
-32,768 to 32,767
|
ushort
|
16 bit
|
0 to 65,535
|
int
|
32 bit
|
-2,147,483,648 to 2,147,483,647
|
uint
|
32 bit
|
0 to 4,294,967,295
|
long
|
64 bit
|
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
|
ulong
|
64 bit
|
0 to 18,446,744,073,709,551,615
|
char
|
16 bit
|
0 to 65535
|
float
|
32 bit
|
-1.5 x 1045 to 3.4 x 1038
|
double
|
64 bit
|
-5 x 10324 to 1.7 x 10308
|
decimal
|
128 bit
|
-1028 to 7.9 x 1028
|
bool
|
---
|
True or false
|
Reference Types:
Contain the address of the memory where data is stored. The
following figure shows the memory allocation of a string value HELLO in a
variable named Str.
String name="Amit"
String Str="HELLO"
String name="Amit"
String Str="HELLO"
Data Types
|
Size
|
Values
|
string
|
Variable length
|
0-2 billion Unicode characters
|
object
|
---
|
---
|
C# also supports dynamic data type.
A variable declared with the dynamic data type can be used
to store a value of any type.
dynamic data=10
id =
“A001”
id=10.5
Complete Example
using System;
namespace Variable
{
class Program
{
static void Main(string[] args)
{
//cretaing
integer type variable
int age;
//cretaing
string type variable
string name;
//cretaing
float type variable
float fees;
//cretaing
gender type variable
char gender;
//Displaying
message
Console.WriteLine("Please enter
age");
//Accepting
Value in age
// age =
Convert.ToInt32(Console.ReadLine());
//we can
use also use Convert.ToInt32 instead of Int32.Parse
age = Int32.Parse(Console.ReadLine());
//Displaying
message
Console.WriteLine("Please enter
name");
//Accepting
Value in name
name = Console.ReadLine();
//Displaying
message
Console.WriteLine("Please enter
fees");
//Accepting
Value in fees
fees =float.Parse(Console.ReadLine());
//Displaying
message
Console.WriteLine("Enter Gender
M or F");
//Accepting
Value
gender = char.Parse(Console.ReadLine());
//displaying
data
Console.WriteLine(" name: {0}\n
age: {1}\n fees: {2}\n Gender: {3}", name,
age, fees,gender); //Output
Console.ReadLine();
}
}
}
If you want to display more than one variable values then you will have to assign place holder for each variables. In the preceding line {0} denotes to name, {1} denotes to age and {2} denotes to fees and so on.
If you want to display more than one variable values then you will have to assign place holder for each variables. In the preceding line {0} denotes to name, {1} denotes to age and {2} denotes to fees and so on.
Conversion
C#
accepts string value by default. If you are using other value then you will
have to convert of specific data types.
age = Int32.Parse(Console.ReadLine());
or
age = Convert.ToInt32(Console.ReadLine());
we can
use the following method to convert one data type to another data type.
int -> int.Parse()
int -> Convert.ToInt32() :- Converts the value provided by the user to int data type.
Double=Convert.ToDouble() :- Converts the value provided by the user to Double data type.
Decimal=Convert.ToDecimal() :- Converts the value provided by the user to Decimal data type.
Byte=Convert.ToByte() :- Converts the value provided by the user to Byte data type.
No comments:
Post a Comment