You are on page 1of 80

EXCEPTIONS AND OBJECT

LIFE TIME
Mr. G.C.Deshpande
Lecturer, CSE@KLS.GIT

Topic Level Objectives


To confabulate about handling runtime anomalies in
the code base through the use of structured
exception handling.
To examine the memory management process
using the .NET garbage collector. To familiarize the
C# keywords such as try, catch, throw and finally.
To examine the distinction between applicationlevel and system-level exceptions.
To inspect various tools within Visual Studio 2012 to
debug the exceptions.
To elucidate the role of application roots, object
generations, and the System.GC type.
To confabulate disposable objects (via the
IDisposable interface) and finalization process (via

Intended Learning Outcomes


1. Investigate various tools within Visual Studio 2012 to debug the exceptions [L 4].
2. Manage the memory using .NET garbage collector [L 3].
3. Clarify the distinction between classes, objects and references [L 2].
4. Explain the role of application roots [L 2].
5. Determine the objects reachable by application roots [L 2].
6. Describe System.GC members and their meaning in life [L 1].
7. Illustrate select members of System.GC type [L 5].
8. Explain the finalization process [L 2].
9. Implement finalizable and disposable objects [L 3].
10. Explain a formalized diaposal pattern [L 2].
11. Define anomaly-centric terms [L 1].
12. Explain the role of .NET exception handling and its atoms [L 2].
13. Explain various properties in configuring the state of an exception [L 2].
14. Differentiate between system level exceptions and application level exceptions [L 4].
15. Build custom/application level exceptions [L 5].
16. Explain the processing of multiple exceptions and the finally block [L 2].
17. Demonstrate the debugging of unhandled exceptions using visual studio 2012 [L 3].

Structured Exception Handling


[SEH]
Bugs: Error made by the programmer
User Errors: Error made by the user
Exceptions: Really exceptional circumstances !!!.. Runtime

anomalies that are difficult to account for while


programming your application.
SEH : Dealing with runtime anomalies.

Role of .NET Exception Handling

Structured Exception Handling


[SEH]
Lot of Numerical codes
Lack of symmetry
SEH : Unified approach to exceptional handling common to

all languages
Human readable description of errors

The Atoms of .NET Exception


Handling

The System.Exception Base Class

The System.Exception Base Class

Core members of
System.Exception type

Core members of
System.Exception type

The simplest possible example

The simplest possible example

The simplest possible example

The simplest possible example

The simplest possible example

Throwing a General Exception

Catching Exceptions

Catching Exceptions

Catching Exceptions

Defining the Pillars of OOP


Encapsulation: How does this language hide an

objects internal implementation details and


preserve data integrity?
Inheritance: How does this language promote
code reuse?
Polymorphism: How does this language let you
treat related objects in a similar way?

Role of Encapsulation

Role of Inheritance [ is-a ]

Role of Inheritance [ has-a ]

Role of Polymorphism

Role of Polymorphism

C# Access Modifiers

C# Access Modifiers

The first pillar: Encapsulation

The first pillar: Encapsulation


Objects internal data should not be directly

accessible from an object instance


Alter the state of an object indirectly using
Accessors (get) and Mutators (set)
.Net property

Black Box Programming

Traditional Encapsulation

Traditional Encapsulation

Encapsulation using Accessors and


Mutators

Encapsulation using Accessors and


Mutators

Encapsulation using .Net


Properties

Encapsulation using .Net


Properties

Encapsulation using .Net


Properties

Encapsulation using .Net


Properties

Controlling Visibility Levels of


Properties

read-only : omit the set block


write-only : omit the get block

Chaining of Constructors

Chaining of Constructors

Basic Mechanics of Inheritance

Basic Mechanics of Inheritance

Basic Mechanics of Inheritance

The sealed keyword

Inheritance

Inheritance

Controlling the base class creation

Controlling the base class creation

Controlling the base class creation


Some properties may be readonly
Inefficient way of creating a constructor: since base class

Default constructor is called before the logic of derived class constructor

Controlling the base class creation

Arrays in C#
An array is a set of data items, accessed using a numerical index
An array is a set of contiguous data points of the same type

Arrays in C#

C# Array Initialization Syntax

Implicitly typed local arrays

Array of objects

Multidimensional Arrays
[ Rectangular ]

Declaration
int[,] myArray = new int[4,2];
int[,,] myArray = new int [4,2,3];

Initialization
int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6},
{7,8}};
int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
int[,] myArray;
myArray = new int[,] {{1,2}, {3,4}, {5,6},
{7,8}};
// OK
myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
// Error

Multidimensional Arrays
[ Rectangular ]

Multidimensional Arrays [ Jagged ]


A jagged array is an array whose elements are arrays.
The elements of a jagged array can be of different dimensions and

sizes.
A jagged array is sometimes called an "array-of-arrays.

Declaration
int[][] myJaggedArray = new int[3][];
Initialization
myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];
myJaggedArray[0] = new int[] {1,3,5,7,9};
myJaggedArray[1] = new int[] {0,2,4,6};
myJaggedArray[2] = new int[] {11,22};

Multidimensional Arrays [ Jagged ]


Initialization

int[][] myJaggedArray = new int [][] {


new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}

};
int[][] myJaggedArray = {
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}

};

Multidimensional Arrays [ Jagged ]


Initialization

// Assign 33 to the second element of the first array:


myJaggedArray[0][1] = 33;
// Assign 44 to the second element of the third
array: myJaggedArray[2][1] = 44;
Mixing of arrays
int[][,] myJaggedArray = new int [3][,]
{
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
};

Multidimensional Arrays [ Jagged ]

The System.Array Base Class

Methods and Parameter Modifiers

Methods and Parameter Modifiers

Default Parameter Passing


Behavior

The out Modifier

The out Modifier

The ref Modifier

The params Modifier

The params Modifier

Defining Optional Parameters

Defining Optional Parameters

Value Types and Reference Types

Value Types and Reference Types

Boxing
Conversion from value type to reference type
int m=100;
object om=m; //creates a box to hold m
int m=100;
object om=(object)m; //c-style casting
int m=10;
object om=m;
m=20;
console.writeline(m); //m=20
console.writeline(om); //om=10

Unboxing
Conversion from reference type to value type
int m=10;
object om=m; //box m
int n=(int)om; //unbox om
int m=500;
object om=m
byte n=(byte)om; // run-time error
Remember: We can only unbox a variable that has
previously been boxed

References
1] Andrew Troelsen, Pro C#with.NET 3.0, SpecialEdition,
Dream tech Press, India, 2007.
2]E. Balagurusamy, Programming in C#, 5thReprint, Tata
McGraw Hill,
2004(For Programming Examples).
3] Tom Archer, Inside C#, WP Publishers, 2001.
4]Herbert Schildt,C#: The Complete Reference, TMH,
2004.
5] Yashavant Kanetkar, C#.NET fundas, First Edition, BPB
Publications,
2002

Contact Me
Email: gcdeshpande@git.edu
gcdeshpande@hotmail.com
Blog: gcdeshpande.spaces.live.com
Follow on twitter:
www.twitter.com/gcdeshpande

You might also like