C # tutorial 003 Data Type & Variable
In .Net framework variables are mainly two types. Value type, Reference type, another type is pointer type. Any application written over the .Net Framework has some memory, called an Application memory. This memory are divided into two parts, Stack and Heap. Stack is a type of memory which operate in FIFO order , that is fast come fast out. We can compare FIFO operation as a dinner plate. The last dinner plate will be pull out first and then on from a queue.
Stack store difference type variable in it . When a function or method is called, all variable related to this function or method are called, or pushed push into the stack and when the function terminate , variable goes out of scope.To work with variable, first step to declare the variable and then to initialism with the value.
int a;
a= 2;
It means I am telling the computer to allocate appropriate amount of memory for integer variable and value 2 will be stored in the memory.
For more example of variable declaration
Here is the list of Value type Variable
Reference type variable :
When a reference type variable is created , a variable is created on the Stack and actual object is created on the heap . The variable is a pointer to the actual object only.When the variable is called , the variable return this of the memory of heap .When the variable goes out of scope the, the actual object does not destroyed ,if it has any other reference ,but if there's no other reference variable it is cleared by garbage collection methods, which is a part of dot net component.
The list of reference type : class, delegates, interface, array.
Example of Reference Type :
//Variable declaration of Student type
Pointer type : A pointer variable is a variable which pointer of the actual variable type. For example.
C # does not support pointer conversion boxing, unboxing. The pointer variable only can be convert to pointer type and integral type. Pointer variable does not support structure details.
Here is example of pointer to integer conversion.
Here is example of integer to pointer conversion.
Conversion type : Conversion is a feature where variable can be converted from one type to another type. There are two type of in C#. Implicit conversion an explicit conversion.
Implicit conversion : Variable can be converted to another variable without loss of data , for example int to long. If you initialism int variable 100, named it a. Another variable named b, declared as a type of long named b. Now if you write b equal to a ,the 100 will be allocated to b. This is called implicit conversion. This type of Type casting done by the compiler for one or more sub type another super type.
Explicit conversion : Sometimes implicit conversion is not possible. If we declare a long variable and integer variable. We assign a large data to long variable and try to convert it to integer, where the integer has a range less than long variable. The full range of long variable will not fit in the integer variable, this cause data will be lost, this type of conversation is called explicit conversion. Then we can say the explicit conversion is not safe, the following result me occur.
Boxing : When a value type is converted to reference type , the process is called boxing and reverse process is called unboxing.
What is a variable? variable storage location what does application store data and read when required.
Variable has a symbolic name and some types of data stored into it. The variable have a capacity,
quantity for storing the data.
Integral : Integral is a term who is represented whole number. If you're right 12 it is a present a
whole number
Sign : Sign means means positive or negative numbers. Define variable means positive or negative
number can be defined and stood in it. For example I can assign -12 to assign variable. But not 12
unsigned variable.
Here are the data types that C# supports
Value types
1) Simple -signed integral sbyte / short / int / long
2) Unsigned integral byte / unshort / uint /ulong
3) Unicode : char
4) IEEE standard double / float
5) Boolean : bull
6) Enum type : any user defined type of data structure which can be used for data
manipulation purpose.
7)Struct type data can be defined by user data manipulation.
Reference type
1) class type
2) Interface :User define type
3) Array type : Signed & multi dimensional
Here is some top topic get it to the variable asked by the interviewer during the interview.
String is used for calling string class method , string is a variable type.
2) Swap two variable without using the third variable
3)Convert.ToString and ToString who is handle the null and which throw exception ?
Convert.ToString handle null. If there is a null value in a variable Convert.ToString string handle
smartly. And it is a type safe conversion process. Tostring is a process, which cannot handle null
and throw null reference exception if null .
4) Another very common practice during the entire conversion is TryParse. These methods parse
string to integer value. This method has different implementation, depending upon the usage.
If we pass string which cannot be converted into integer, the method will throw exception.
5) Another type of implementation this method is out number. After conversion the method return in
a variable true or false. If the conversion is successful then the method return true, if the method
and successful , return false.
In .Net framework variables are mainly two types. Value type, Reference type, another type is pointer type. Any application written over the .Net Framework has some memory, called an Application memory. This memory are divided into two parts, Stack and Heap. Stack is a type of memory which operate in FIFO order , that is fast come fast out. We can compare FIFO operation as a dinner plate. The last dinner plate will be pull out first and then on from a queue.
Stack store difference type variable in it . When a function or method is called, all variable related to this function or method are called, or pushed push into the stack and when the function terminate , variable goes out of scope.To work with variable, first step to declare the variable and then to initialism with the value.
int a;
a= 2;
It means I am telling the computer to allocate appropriate amount of memory for integer variable and value 2 will be stored in the memory.
For more example of variable declaration
int
a; //Interger
type
string
a; //string
type type
bool
a; //boolean
type , true false
long
a; //long
type
object
a; //object
type
decimal
a; //decimal
type
double
a; //double
type
float
a; //float
type
decimal
a; //decimal
type
Store value into variable
int
a=100;
string
a="Hello";
bool
a=true;
long
a=1234578963;
object
a=new
Student ();
decimal
a=12.56654;
double
a=154.5656;
float
a=15.54;
Here is the list of Value type Variable
Type
|
Size
|
Range
|
sbyte
|
8
|
-128 to 127
|
byte
|
8
|
0 to 255
|
short
|
16
|
-32768 to 32767
|
ushort
|
16
|
0 to 65535
|
int
|
32
|
-2147483648 to 2147483647
|
uint
|
32
|
0 to 4294967295
|
long
|
64
|
-9223372036854775808 to 9223372036854775807
|
ulong
|
64
|
0 to 18446744073709551615
|
char
|
16
|
0 to 65535
|
float
|
32
|
7 digits
|
double
|
64
|
15-16 digits
|
decimal
|
128
|
28-29 decimal places
|
char
|
Char
|
A single Unicode character
|
bool
|
Boolean
|
Logical Boolean type
|
object
|
Object
|
Base type of all other types
|
string
|
String
|
A sequence of characters
|
Reference type variable :
When a reference type variable is created , a variable is created on the Stack and actual object is created on the heap . The variable is a pointer to the actual object only.When the variable is called , the variable return this of the memory of heap .When the variable goes out of scope the, the actual object does not destroyed ,if it has any other reference ,but if there's no other reference variable it is cleared by garbage collection methods, which is a part of dot net component.
The list of reference type : class, delegates, interface, array.
Example of Reference Type :
public
class
Student
{
public
string
Name { get;
set;
}
public
int
Age { get;
set;
}
public
Student(string
name, int
age)
{
Name
= name;
Age
= age;
}
//....other
function and methods
}
//Variable declaration of Student type
Student
std1 = new
Student()
Pointer type : A pointer variable is a variable which pointer of the actual variable type. For example.
int*
x;
int*
y;
* mark denotes address of operator and & returns the address of a variable .C # does not support pointer conversion boxing, unboxing. The pointer variable only can be convert to pointer type and integral type. Pointer variable does not support structure details.
Here is example of pointer to integer conversion.
int*
pointer;
int
a;
a
= &pointer;
Here is example of integer to pointer conversion.
int*
pointer;
int
a;
void*
pointer = &a;
Conversion type : Conversion is a feature where variable can be converted from one type to another type. There are two type of in C#. Implicit conversion an explicit conversion.
Implicit conversion : Variable can be converted to another variable without loss of data , for example int to long. If you initialism int variable 100, named it a. Another variable named b, declared as a type of long named b. Now if you write b equal to a ,the 100 will be allocated to b. This is called implicit conversion. This type of Type casting done by the compiler for one or more sub type another super type.
int
a=100;
long
b;
b
= a;
Explicit conversion : Sometimes implicit conversion is not possible. If we declare a long variable and integer variable. We assign a large data to long variable and try to convert it to integer, where the integer has a range less than long variable. The full range of long variable will not fit in the integer variable, this cause data will be lost, this type of conversation is called explicit conversion. Then we can say the explicit conversion is not safe, the following result me occur.
- Risk of data loss
- Throw error
- No error but loss of data
- Cast operators needed for Type casting.
long
b = 10000000000000;
int
a = (int)b;
Boxing : When a value type is converted to reference type , the process is called boxing and reverse process is called unboxing.
C # define a list of inversion method for conversion to a particular type, these are
* ToBoolean: To convert into Boolean type, that is true or false.
string str = "1";
bool a = Convert.ToBoolean("1");
string str = "1";
bool a = Convert.ToBoolean("1");
*ToByte : Convert into byte type or binary type.
string
str = "1";
byte
a = Convert.ToByte("1");
*ToChar : To convert into character type .
string
str = "1";
char
a = Convert.ToChar("1");
* ToDateTime :To convert into date time .
string
str = "1";
DateTime
a = Convert.ToDateTime("1");
* ToDouble : To convert into double type.
string
str = "1";
double
a = Convert.ToDouble("1");
* Toint16 / 32 / 64 : To convert into integer long type depending upon the data length.
string
str = "1";
int
a = Convert.ToInt16("1");
* ToSingle : To convert into the single type date to string to convert into the string type.
string
str = "1";
Single
a = Convert.ToSingle("1");
Conversion Type : Conversion Type are two types.
Boxing : Boxing is a feature of of.Net architecture which can be useful during the conversion.
Boxing actually convert from value type to reference type. It is implicit conversion. Which a object is
boxed, memory is allocated on the heap for the new object, name the values related variable to the
actual object variable. For example I am declaring a integer type I, I am a sign it to a value hundred,
now I am declaring an object type variable o. Now I am writing, o=I. Now the conversion has done
and the value of a is assigned to the object o.
Unboxing : Unboxing is a technique just reverse boxing , which converts reference type to
value type. It is an explicit conversion, which means data can be lost during conversion. When
an hour is done, value from the object is copied to a variable which is reference type.
Boxing : Boxing is a feature of of.Net architecture which can be useful during the conversion.
Boxing actually convert from value type to reference type. It is implicit conversion. Which a object is
boxed, memory is allocated on the heap for the new object, name the values related variable to the
actual object variable. For example I am declaring a integer type I, I am a sign it to a value hundred,
now I am declaring an object type variable o. Now I am writing, o=I. Now the conversion has done
and the value of a is assigned to the object o.
int
I = 100;
object
o;
o
= I;
Unboxing : Unboxing is a technique just reverse boxing , which converts reference type to
value type. It is an explicit conversion, which means data can be lost during conversion. When
an hour is done, value from the object is copied to a variable which is reference type.
object
o;
int
i = (int)o;
What is a variable? variable storage location what does application store data and read when required.
Variable has a symbolic name and some types of data stored into it. The variable have a capacity,
quantity for storing the data.
Integral : Integral is a term who is represented whole number. If you're right 12 it is a present a
whole number
Sign : Sign means means positive or negative numbers. Define variable means positive or negative
number can be defined and stood in it. For example I can assign -12 to assign variable. But not 12
unsigned variable.
Here are the data types that C# supports
Value types
1) Simple -signed integral sbyte / short / int / long
2) Unsigned integral byte / unshort / uint /ulong
3) Unicode : char
4) IEEE standard double / float
5) Boolean : bull
6) Enum type : any user defined type of data structure which can be used for data
manipulation purpose.
7)Struct type data can be defined by user data manipulation.
Reference type
1) class type
- object
- string
- user defined
2) Interface :User define type
3) Array type : Signed & multi dimensional
1) String
and string , what is the difference between them?
String is used for calling string class method , string is a variable type.
string
a="hello";
string
b = String.Concat(a,
"world");
Response.Write(b);
//hellow world
2) Swap two variable without using the third variable
int
a, b;
a
= 10;
b
= 20;
a
= a + b;
b
= a - b;
a
= a - b;
//a=20
//b=10
3)Convert.ToString and ToString who is handle the null and which throw exception ?
string
a=null;
string
b=Convert.ToString(a);
string
c = a.ToString();
Convert.ToString handle null. If there is a null value in a variable Convert.ToString string handle
smartly. And it is a type safe conversion process. Tostring is a process, which cannot handle null
and throw null reference exception if null .
4) Another very common practice during the entire conversion is TryParse. These methods parse
string to integer value. This method has different implementation, depending upon the usage.
If we pass string which cannot be converted into integer, the method will throw exception.
string
input = "100";
int
output = int.Parse(input);
Response.Write(output);
//output
:100
string
input = "hellow";
int
output = int.Parse(input);
Response.Write(output);
//output
:error ,Input string was not in a correct format.
5) Another type of implementation this method is out number. After conversion the method return in
a variable true or false. If the conversion is successful then the method return true, if the method
and successful , return false.
string
input = "100";
int
output;
bool
result = int.TryParse(input,
out
output);
if
(result == true)
{
Response.Write(output);
}
Output :
100
100
No comments:
Post a Comment