06cs761 ppts chapter iii

64
Mr. G.C.Deshpande Lecturer, [email protected]

Upload: gajendra-deshpande

Post on 14-Dec-2015

219 views

Category:

Documents


3 download

DESCRIPTION

C# Programming and .Net Unit 3

TRANSCRIPT

Page 1: 06cs761 Ppts Chapter III

Mr. G.C.Deshpande

Lecturer, [email protected]

Page 2: 06cs761 Ppts Chapter III

Topic Level ObjectivesTopic Level ObjectivesTo 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.

Page 3: 06cs761 Ppts Chapter III

Intended Learning OutcomesIntended Learning Outcomes1. 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].

Page 4: 06cs761 Ppts Chapter III

The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram

Page 5: 06cs761 Ppts Chapter III

The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram

Page 6: 06cs761 Ppts Chapter III

The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram

Page 7: 06cs761 Ppts Chapter III

Variations on the Main() Variations on the Main() MethodMethod

Page 8: 06cs761 Ppts Chapter III

Processing Command-Line Processing Command-Line ArgumentsArguments

Page 9: 06cs761 Ppts Chapter III

Processing Command-Line Processing Command-Line ArgumentsArguments

Page 10: 06cs761 Ppts Chapter III

Processing Command-Line Processing Command-Line ArgumentsArguments

Page 11: 06cs761 Ppts Chapter III

C# Iteration ConstructsC# Iteration Constructs

for loop foreach loop while loop do….while loop

Page 12: 06cs761 Ppts Chapter III

The for loopThe for loop

Iteration over block of code for fixed number of times

Page 13: 06cs761 Ppts Chapter III

The foreach loopThe foreach loop

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

Page 14: 06cs761 Ppts Chapter III

The while loopThe while loop

Iteration over block of code until some terminating condition

Page 15: 06cs761 Ppts Chapter III

The do….while loopThe do….while loop

Iteration over block of code for undetermined number of times

Page 16: 06cs761 Ppts Chapter III

Decision ConstructsDecision Constructs

The if/else statement The switch statement

Page 17: 06cs761 Ppts Chapter III

The if/else statementThe if/else statement

Page 18: 06cs761 Ppts Chapter III

The switch statement The switch statement

Page 19: 06cs761 Ppts Chapter III

The switch statementThe switch statement

Page 20: 06cs761 Ppts Chapter III

EnumerationsEnumerations

Page 21: 06cs761 Ppts Chapter III

EnumsEnums

Page 22: 06cs761 Ppts Chapter III

Enums in switch statementEnums in switch statement

Page 23: 06cs761 Ppts Chapter III

Enums in switch statementEnums in switch statement

Page 24: 06cs761 Ppts Chapter III

Structures Structures

Page 25: 06cs761 Ppts Chapter III

Structures Structures

(methods, properties)

Page 26: 06cs761 Ppts Chapter III

Structures – creating variablesStructures – creating variables

Page 27: 06cs761 Ppts Chapter III

Structs with custom constructorStructs with custom constructor

Page 28: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 29: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 30: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 31: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 32: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 33: 06cs761 Ppts Chapter III

Verbatim StringsVerbatim Strings

Page 34: 06cs761 Ppts Chapter III

Strings in C#Strings in C#

Page 35: 06cs761 Ppts Chapter III

Strings are immutableStrings are immutable

Page 36: 06cs761 Ppts Chapter III

Arrays in C#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

Page 37: 06cs761 Ppts Chapter III

Arrays in C#Arrays in C#

Page 38: 06cs761 Ppts Chapter III

C# Array Initialization SyntaxC# Array Initialization Syntax

Page 39: 06cs761 Ppts Chapter III

Implicitly typed local arraysImplicitly typed local arrays

Page 40: 06cs761 Ppts Chapter III

Array of objectsArray of objects

Page 41: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Rectangular ][ Rectangular ]Declarationint[,] myArray = new int[4,2];

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

Initializationint[,] 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}}; // OKmyArray = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error

Page 42: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Rectangular ][ Rectangular ]

Page 43: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Jagged ][ 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};

Page 44: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Jagged ][ 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}

};

Page 45: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Jagged ][ 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 arraysint[][,] 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} }

};

Page 46: 06cs761 Ppts Chapter III

Multidimensional Arrays Multidimensional Arrays [ Jagged ][ Jagged ]

Page 47: 06cs761 Ppts Chapter III

The System.Array Base ClassThe System.Array Base Class

Page 48: 06cs761 Ppts Chapter III

Methods and Parameter Methods and Parameter ModifiersModifiers

Page 49: 06cs761 Ppts Chapter III

Methods and Parameter Methods and Parameter ModifiersModifiers

Page 50: 06cs761 Ppts Chapter III

Default Parameter Passing Default Parameter Passing BehaviorBehavior

Page 51: 06cs761 Ppts Chapter III

The out ModifierThe out Modifier

Page 52: 06cs761 Ppts Chapter III

The out ModifierThe out Modifier

Page 53: 06cs761 Ppts Chapter III

The ref ModifierThe ref Modifier

Page 54: 06cs761 Ppts Chapter III

The params ModifierThe params Modifier

Page 55: 06cs761 Ppts Chapter III

The params ModifierThe params Modifier

Page 56: 06cs761 Ppts Chapter III

Defining Optional ParametersDefining Optional Parameters

Page 57: 06cs761 Ppts Chapter III

Defining Optional ParametersDefining Optional Parameters

Page 58: 06cs761 Ppts Chapter III

Value Types and Reference Value Types and Reference TypesTypes

Page 59: 06cs761 Ppts Chapter III

Value Types and Reference Value Types and Reference TypesTypes

Page 60: 06cs761 Ppts Chapter III

BoxingBoxingConversion 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

Page 61: 06cs761 Ppts Chapter III

UnboxingUnboxingConversion 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

Page 62: 06cs761 Ppts Chapter III

ReferencesReferences

1] Andrew Troelsen, Pro C# with .NET 3.0, Special Edition,

Dream tech Press, India, 2007.

2] E. Balagurusamy, Programming in C#, 5th Reprint, 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

 

Page 63: 06cs761 Ppts Chapter III

Contact MeContact Me

Email: [email protected]

[email protected]

Blog: gcdeshpande.spaces.live.com

Follow on twitter: www.twitter.com/gcdeshpande

Page 64: 06cs761 Ppts Chapter III