Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Unit 1

Prof. Rohan kishor parab


prof. Rohan kishor parab
Value Type and a Reference Type

● The Types in .NET Framework are either treated


by Value Type or by Reference Type.
● A Value Type holds the data within its own
memory allocation
● A Reference Type contains a pointer to another
prof. Rohan kishor parab

memory location that holds the real data


● Reference Type variables are stored in the heap
while Value Type variables are stored in the stack
Prof. Rohan kishor parab
Value Type:

● A Value Type stores its contents in memory


allocated on the stack.
● Predefined data types, structures, enums are also
value types, and work in the same way.
● Value types can be created at compile time and
Stored in stack memory, because of this,
Prof. Rohan kishor parab

Garbage collector can't access the stack.


● e.g.
Reference Type:

● Reference Types are used by a reference which


holds a reference (address) to the object but not
the object itself.
● reference types represent the address of the
variable rather than the data itself
● e.g.
Prof. Rohan kishor parab

● In the above code the space required for the 20


integers that make up the array is allocated on
the heap.
Stack and a Heap

● Stack is used for static memory allocation and


Heap for dynamic memory allocation, both stored
in the computer's RAM .
● Variables allocated on the stack are stored
directly to the memory and access to this memory
is very fast
Prof. Rohan kishor parab

● Variables allocated on the heap have their


memory allocated at run time and accessing this
memory is a bit slower
Boxing In C#

● The process of Converting a Value Type (char,


int etc.) to a Reference Type(object) is
called Boxing.
● Boxing is implicit conversion process in which
object type (super type) is used.
The Value type is always stored in Stack.
Prof. Rohan kishor parab


Unboxing In C#

● The process of converting reference type into


the value type is known as Unboxing.
● It is explicit conversion process.
Prof. Rohan kishor parab
Delegates
● C# delegates are similar to pointers to functions

● All delegates are implicitly derived from


the System.Delegate class.
prof. Rohan kishor parab

● delegate is a reference type variable that holds


the reference to a method.

● The reference can be changed at runtime.


Declaring Delegates
● Delegate declaration determines the methods
that can be referenced by the delegate
● A delegate can refer to a method, which have the
same signature as that of the delegate.
● Syntax for delegate declaration is:
delegate<return type><delegate-name><parameter list>
prof. Rohan kishor parab

● For example:
– public delegate int MyDelegate (string s);
Multicasting of a Delegate

● Delegate objects can be composed using the "+"


operator.

● A composed delegate calls the two delegates it was


composed from
prof. Rohan kishor parab

● Only delegates of the same type can be composed.

● The "-" operator can be used to remove a component


delegate from a composed delegate.
Events
● Events are basically a user action like key press,
clicks, mouse movements, etc
● Applications need to respond to events when
they occur
● Events and delegates work hand-in-hand to
provide a program's functionality.
prof. Rohan kishor parab

● The events are declared and raised in a class


and associated with the event handlers using
delegates within the same class or some other
class
Prof. Rohan kishor parab

You might also like