unit 3
unit 3
NET
Prepared by:
Sumit Chhillar
(Assistant Professor)
SYLLABUS
UNIT III
• There are few other important operators including sizeof, typeof and ? : supported
by C#.
Garbage Collection in C#
| .NET Framework
• Automatic memory management is made possible
by Garbage Collection in .NET Framework. When a class
object is created at runtime, certain memory space is allocated
to it in the heap memory. However, after all the actions related
to the object are completed in the program, the memory space
allocated to it is a waste as it cannot be used. In this case,
garbage collection is very useful as it automatically releases
the memory space after it is no longer required.
• Garbage collection will always work on Managed Heap and
internally it has an Engine which is known as
the Optimization Engine.
• Garbage Collection occurs if at least one of multiple
conditions is satisfied. These conditions are given as follows:
• If the system has low physical memory, then garbage
collection is necessary.
• If the memory allocated to various objects in the heap memory
exceeds a pre-set threshold, then garbage collection occurs.
• If the GC.Collect method is called, then garbage collection
occurs. However, this method is only called under unusual
situations as normally garbage collector runs automatically.
• Marking Phase: A list of all the live objects is created during the marking
phase. This is done by following the references from all the root objects.
All of the objects that are not on the list of live objects are potentially
deleted from the heap memory.
• Relocating Phase: The references of all the objects that were on the list of
all the live objects are updated in the relocating phase so that they point to
the new location where the objects will be relocated to in the compacting
phase.
• Compacting Phase: The heap gets compacted in the compacting phase as
the space occupied by the dead objects is released and the live objects
remaining are moved. All the live objects that remain after the garbage
collection are moved towards the older end of the heap memory in their
original order.
• Generation 0 : All the short-lived objects such as temporary variables are
contained in the generation 0 of the heap memory. All the newly allocated
objects are also generation 0 objects implicitly unless they are large
objects. In general, the frequency of garbage collection is the highest in
generation 0.
• Generation 1 : If space occupied by some generation 0 objects that are not
released in a garbage collection run, then these objects get moved to
generation 1. The objects in this generation are a sort of buffer between the
short-lived objects in generation 0 and the long-lived objects in generation
2.
• Generation 2 : If space occupied by some generation 1 objects that are not
released in the next garbage collection run, then these objects get moved to
generation 2. The objects in generation 2 are long lived such as static
objects as they remain in the heap memory for the whole process duration.
C# - Indexers
• System.IO.IOException
Handles I/O errors.
• System.IndexOutOfRangeException
Handles errors generated when a method refers to an array index out of
range.
• System.ArrayTypeMismatchException
Handles errors generated when type is mismatched with the array type.
• System.NullReferenceException
Handles errors generated from referencing a null object.
• System.DivideByZeroException
Handles errors generated from dividing a dividend with zero.
• System.InvalidCastException
Handles errors generated during typecasting.
• System.OutOfMemoryException
Handles errors generated from insufficient
free memory.
• System.StackOverflowException
Handles errors generated from stack overflow.
Handling Exceptions
C# - Decision Making
• Unlike for and while loops, which test the loop condition at the start of the
loop, the do...while loop checks its condition at the end of the loop.
• A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
• Syntax
• The syntax of a do...while loop in C# is −
• do { statement(s); } while( condition );
• Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
• If the condition is true, the flow of control jumps back up to do, and the
statement(s) in the loop execute again. This process repeats until the given
condition becomes false
C# - Arrays