You are on page 1of 26

Literals in C#

i. The Literals in C# are the fixed values given to your variable


and these values cannot be modified during the execution of the
program.
ii. The fixed values are called Literals in C#.
iii. Literal is a value that is used by the variables.

For example, int x = 100;


Here x is a variable, and 100 is literal.
Types of Literals in C#
What is meant by Value Type and Reference Types?
Value Types:
i. The data types which store the data directly into their memory
locations is known as Value Types.
ii. In this memory allocated at compile time

iii. Memory allocation is made with in the Stack.


iv. CLR does not provide Automatic Memory Management.
Reference Type:
• The data types which do not store the data directly into their
memory locations rather refer to other memory location is known as
Reference Types.

• In this memory allocated at Run Time.


• Memory allocation is made with in the Heap
Value Types versus Reference Types
C# has two kinds of data types, value types and reference types.

• Value type stores the value itself, whereas the reference type stores
the address of the value where it is stored.
• Some predefined data types such as
int, float, double, decimal, bool, char, etc. are value types and
 object, string, and array are reference types.
• While working with these data types, you often need to convert
value types to reference types or vice-versa.
• Because, both have different characteristics and .NET stores them
differently in the memory, it must do some work internally to
convert them from one type to another.
• These conversion processes are called boxing and UnBoxing.
Value Type
Reference Type
To Print Maximum and Minimum Values of Data types
static void Main(string[] args)
{
Console.WriteLine("Integer Maximum value is " + int.MaxValue);
Console.WriteLine();
Console.WriteLine("Integer Minimumvalue is " + int.MinValue);
Console.WriteLine();
Console.WriteLine("Float Maximum value is " + float.MaxValue);
Console.WriteLine();
Console.WriteLine("Float Minimum value is " + float.MinValue);
Console.WriteLine();
Console.WriteLine("Double Maximum value is " + double.MaxValue);
Console.WriteLine();
Console.WriteLine("Double Minimum value is " + double.MinValue);
Console.WriteLine();
Console.ReadLine();
}
}
}
What is boxing?
1. Boxing is the process of converting a value type to the
object type or any interface type implemented by this
value type.
2. Boxing is implicit.
Implicit:
 In C#, Create local variable without specifying its
type.
The C# var keyword is used to create implicit typed
local variables.
The C# compiler infers the types of variable on the basis
of assigned value.
int i = 10;
object o = i; //performs boxing

In the above example, the integer variable i is assigned to object o.


Since object type is a reference type and base class of all the classes
in C#, an int can be assigned to an object type.

This process of converting int to object is called boxing.


 All the reference types stored on heap where it
contains the address of the value and value type is just an
actual value stored on the stack.
Now, as shown in the first example, int i is assigned to
object o. Object o must be an address and not a value
itself.
So, the CLR boxes the value type by creating a new
System.Object on the heap and wraps the value of i in it
and then assigns an address of that object to o.
So, because the CLR creates a box on the heap that stores
the value, the whole process is called 'Boxing'.
Line1: int x = 10;
When this statement is executed, an integer variable x will
be created in the Stack memory with a value of 10.
Line2: object y = x;
When this statement is executed, moving the x value i.e. 10 to an
object data type.
If you remember the object is the parent class for all classes
in .NET Framework.
When you move a value type to a reference to type, it is called
Boxing. So, here we are moving value type integer x to
reference type object y.
Line3: int z = (int)y;
• When this statement is executed, moving the object value
to an integer data type by doing type casting. When we
move a reference type to a value type, it is called
Unboxing. So, here moving reference type value i.e. y to
integer type i.e. z, so performing Unboxing here.
What is UnBoxing?
 UnBoxing is the reverse of boxing.
It is the process of converting a reference type to value
type.
UnBoxing extract the value from the reference type and
assign it to a value type.
 UnBoxing is explicit. It means we have to cast explicitly.
• A boxing conversion makes a copy of the value. So,
changing the value of one variable will not impact others.
int i = 10;
object o = i; // boxing
o = 20;
Console.WriteLine(i); //output=?

Unboxing – int y= Convert.ToInt32(obj);


What is meant by Converting?
Working with predefined class called a “Convert” is
called as Converting
 A class contains a collection of methods.
 Converting can be used to convert from any data type
into any other data type.
In C#, you can convert a string representation of a number
to an integer using the following ways:
• Parse() method
• Convert class
Parse Method
The Parse() methods are available for all the primitive
datatypes. It is the easiest way to convert from string to
integer.
The Parse methods are available for 16, 32, 64 bit signed
integer types:
Int16.Parse()
Int32.Parse()
Int64.Parse()
Convert Class
• Another way to convert string to integer is by using
static Convert class.
• The Convert class includes different methods which
convert base data type to another base data type.
The Convert class includes the following methods to
convert from different data types to int type.
• Convert.ToInt16()
• Convert.ToInt32()
• Convert.ToInt64()
• The Convert.ToInt16() method returns the 16-bit integer
e.g. short,
• The Convert.ToInt32() returns 32-bit integers e.g. int and
• The Convert.ToInt64() returns the 64-bit integer e.g.
long.
Convert string to int using Convert class
Convert.ToInt16("100"); // returns short
Convert.ToInt16(null); //returns 0

Convert.ToInt32("233300"); // returns int


Convert.ToInt32("1234",16); // returns 4660 -
Hexadecimal of 1234

Convert.ToInt64("1003232131321321"); //returns long

You might also like