using System;
class VarsDemo
{
static Void main ()
{
int x, y, z;
console.Write(“enter
x value:”);
x =
int.Parse(console.ReadLine());
console.Write(“Enter
y value :”);
y =
int.Parse(console.ReadLine());
z = x+y;
console.WriteLine(“Sum
of {0} & {1} is : {2}”,x,y,z);
}
}
|
The ReadLine method return the value what it
read in string format only because the return type of the method is string.
Parse is the
method that is capable of converting astringe expressions into a given type on
which the method is called provided it is convertible or else raises an error.
Int x =
int.Parse (“100”);
float f =
float.Parse (“3.14”);
bool b =
bool.Parse(“true”);
int y =
int.Parse(“1a7”); // invalid
Data types are classified into two
types
- Value types
- Reference type
Value types store the data in stack which is the
place where data store in fixed length such as int float etc.
- Every program has its own stack and no other program shares it.
- Stack works on the principal “first in last out” and it is under the control of operating system which doesn’t provide automatic memory mgmt but faster in access.
Reference
type – reference type are store on Heap memory which is the other place
where data can be stored. C# support two predefined reference type objects and
strings and .net the Heap is now more managed and called as managed Heap, which
will be under the control of garbage collector.
Nulleble Value
types (2.0):- it is a new feature that has been added in C# 2.0 which allows
storing of null value in value types that is not possible in the version before
2.0, which gives more flexibility while communicating with data bases because
under databases null values can be stores both in value types and reference
types also.
Ex- string Str = null; //valid
Int x = null; //Invalid
Int? x = null; //valid
Note: - to
declare a value type for storing null value we need to suffix the type with
“?”.
Implicitly
typed variable (3.0) :- It is a new feature that has been added in C# 3.0,
which allows declaring a variable using the keyword “var”. the type of declared
variable is identified in run time depending upon the value we assign to the
variable.
Var x = 100; //x is an int variable.
Var f = 3.14; //f is a float variable.
Var s =
“Hello”; // s is a string
variable.
Note- Declaring
a variable here without assigning a value is not allowing.
Var y; // invalid.
Program
using System;
class TypeDemo
{
Static void
{
var x = 100;
console.Write.Line(x.GetType());
var s = “Hello”;
console.WriteLine(s.GetType());
var f = 3.14;
console.WriteLine(f.GetType());
}
}
|
Note :- get
type is a predefined method that return the type of a given variable or object.
Boxing &
Un-boxing :- If at all a value type is assign to a reference type variable and
sent to managed heap we called it as Boxing
int x = 100;
object obj = x; // BOXING
Un Boxing is
reverse a boxing which allows reassign of refrance type back to corresponding
vale type again but here an explicit type casting is required.
int y =
(int)obj; // un - Boxing
No comments:
Post a Comment