06cs761 ppts chapter iii
DESCRIPTION
C# Programming and .Net Unit 3TRANSCRIPT
Mr. G.C.Deshpande
Lecturer, [email protected]
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.
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].
The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram
The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram
The Anatomy of Simple C# The Anatomy of Simple C# ProgramProgram
Variations on the Main() Variations on the Main() MethodMethod
Processing Command-Line Processing Command-Line ArgumentsArguments
Processing Command-Line Processing Command-Line ArgumentsArguments
Processing Command-Line Processing Command-Line ArgumentsArguments
C# Iteration ConstructsC# Iteration Constructs
for loop foreach loop while loop do….while loop
The for loopThe for loop
Iteration over block of code for fixed number of times
The foreach loopThe foreach loop
Iteration over all items within an array without checking the upper limit
The while loopThe while loop
Iteration over block of code until some terminating condition
The do….while loopThe do….while loop
Iteration over block of code for undetermined number of times
Decision ConstructsDecision Constructs
The if/else statement The switch statement
The if/else statementThe if/else statement
The switch statement The switch statement
The switch statementThe switch statement
EnumerationsEnumerations
EnumsEnums
Enums in switch statementEnums in switch statement
Enums in switch statementEnums in switch statement
Structures Structures
Structures Structures
(methods, properties)
Structures – creating variablesStructures – creating variables
Structs with custom constructorStructs with custom constructor
Strings in C#Strings in C#
Strings in C#Strings in C#
Strings in C#Strings in C#
Strings in C#Strings in C#
Strings in C#Strings in C#
Verbatim StringsVerbatim Strings
Strings in C#Strings in C#
Strings are immutableStrings are immutable
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
Arrays in C#Arrays in C#
C# Array Initialization SyntaxC# Array Initialization Syntax
Implicitly typed local arraysImplicitly typed local arrays
Array of objectsArray of objects
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
Multidimensional Arrays Multidimensional Arrays [ Rectangular ][ Rectangular ]
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};
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}
};
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} }
};
Multidimensional Arrays Multidimensional Arrays [ Jagged ][ Jagged ]
The System.Array Base ClassThe System.Array Base Class
Methods and Parameter Methods and Parameter ModifiersModifiers
Methods and Parameter Methods and Parameter ModifiersModifiers
Default Parameter Passing Default Parameter Passing BehaviorBehavior
The out ModifierThe out Modifier
The out ModifierThe out Modifier
The ref ModifierThe ref Modifier
The params ModifierThe params Modifier
The params ModifierThe params Modifier
Defining Optional ParametersDefining Optional Parameters
Defining Optional ParametersDefining Optional Parameters
Value Types and Reference Value Types and Reference TypesTypes
Value Types and Reference Value Types and Reference TypesTypes
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
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
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
Contact MeContact Me
Email: [email protected]
Blog: gcdeshpande.spaces.live.com
Follow on twitter: www.twitter.com/gcdeshpande