You are on page 1of 64

Mr. G.C.

Deshpande
Lecturer, CSE@KLS.GIT

Topic Level Objectives


To examine the core constructs of the C#
programming
language.
To familiarize basic class construction techniques,
the distinction
between value types and reference types, boxing
and unboxing,
and the role of favorite base class, System.Object.
To exemplify various commonplace programming
constructs in
.NET platform, such as flow control, enumerations,
arrays,
structures, classes and string processing.

Intended Learning Outcomes


1. Illustrate the concept of boxing and unboxing [L 5].
2. Explain the role of nullable data types [L 2].
3. Create custom namespaces [L 5].
4. Investigate various aspects of Common Language Runtime [L 4].
5. Explain the basic structure of C# program [L 2].
6. Discuss the variations of Main() method [L 2].
7. Explain the role of System.Object [L 2].
8. Discuss various parameter passing conventions, value types and reference
types [L 2].
9. Define classes and objects, also application object [L 1].
10. Explain the role of constructors [L 2].
11. Discuss Basic input and output methods of console class [L 2].
12. Differentiate between constructors and static constructors [L 4].
13. Explain various Iteration constructs, decision constructs and relational
operators [L 2].
14. Compare iteration constructs of C# with the iteration constructs of other
languages [L 6].

The Anatomy of Simple C#


Program

The Anatomy of Simple C#


Program

The Anatomy of Simple C#


Program

Variations on the Main()


Method

Processing Command-Line
Arguments

Processing Command-Line
Arguments

Processing Command-Line
Arguments

C# Iteration Constructs
for loop
foreach loop
while loop
do.while loop

The for loop


Iteration over block of code for fixed number of
times

The foreach loop


Iteration over all items within an array without checking
the upper limit

The while loop


Iteration over block of code until some terminating
condition

The do.while loop


Iteration over block of code for undetermined
number of times

Decision Constructs
The if/else statement
The switch statement

The if/else statement

The switch statement

The switch statement

Enumerations

Enums

Enums in switch statement

Enums in switch statement

Structures

Structures

(methods, properties)

Structures creating variables

Structs with custom constructor

Strings in C#

Strings in C#

Strings in C#

Strings in C#

Strings in C#

Verbatim Strings

Strings in C#

Strings are immutable

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
[Declaration
Rectangular ]
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
[Initialization
Jagged ]
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
[Initialization
Jagged ]
// 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