200 likes | 394 Views
VPMP POLYTECHNIC,GANDHINAGAR. Introduction to C# Language (C# Basics). Prepared By:- Shweta Patel (Lecturers - Computer Department). Topics Covered In This Presentation. 2.1 Introducing C# Data Types (Including Boxing – Unboxing) 2.2 C# Control structures 2.2.1 Conditional structure
E N D
VPMP POLYTECHNIC,GANDHINAGAR Introduction to C# Language (C# Basics) Prepared By:- Shweta Patel (Lecturers - Computer Department)
Topics Covered In This Presentation 2.1 Introducing C# Data Types (Including Boxing – Unboxing) 2.2 C# Control structures 2.2.1 Conditional structure 2.2.2 Looping structure 2.3 Constructor and Destructor 2.4 Arrays (one dimensional, multidimensional, jagged) 2.5 Understanding Properties and Indexers 2.5.1 Get Accessor, Set Accessor 2.5.2 Indexers (One Dimension) & property
Arrays An array is a collection of variable of the same data type that are referred to by a common name. In C# arrays can have one or more dimensions. Arrays are used for variety of purposes because they offer a convenient means of grouping together related variables. Zero based, type bound Built on .NET System.Array class
Arrays There are three types of Arrays. 1] One-dimensional Arrays 2] Multi-dimensional Arrays -Two-dimensional Arrays -Three or more dimensional Arrays 3] Jagged Arrays
Arrays - Features • Use Length for number of items in array: • nums.Length • Static Array methods: • Sort System.Array.Sort(myArray); • Reverse System.Array.Reverse(myArray); • IndexOf • LastIndexOf • intmyLength = myArray.Length; • System.Array.IndexOf(myArray, “K”, 0, myLength)
Declaring an Array Type [ ] array-name; Example: Int [ ] numbers; numbers = new int[3];
Declaring an Array Array numbering follows C conventions First element is numbers[0] Upper bound is 2, so 3rd element is numbers[2] Technically, just creates a variable (numbers) that will hold a reference to an array-of-integers object. Type [ ] array-name; Example: Int [ ] numbers ; numbers = new int [ 3 ] ; Creates an array-of-integers object, of length 3, which are initialized to 0. numbers 0 0 0
One-dimensional Arrays One-dimensional Arrays is a list of related variables. To declare a One-dimensional Array, you will use this general form. type[ ] array-name = new type[size]; Example: int [ ] sample = new int[10]; int [ ] sample; sample=new int [10];
One-dimensional Arrays using System; Class ArrayDemo { public static void main() { int[ ] sample = new int[10]; int i; for(i=0;i<10;i++) sample[i]=i; for(i=0;i<10;i++) Console.WriteLine(“sample[“+i+”]:”+sample[i]); }}
One-dimensional Arrays public static void main() { int [ ] nums =new int[4]; int avg =0; nums[0]=99; nums[1]=10; nums[0]=18; nums[0]=78; For (int i=0;i<4;i++) Avg = avg + nums [i]; Avg =avg/10; Console.WriteLine (“Average:” +avg); }
One-dimensional Arrays using System; Class ArrayDemo { public static void main() { int [ ] nums={99,10,18,78}; int avg =0; For (int i=0;i<4;i++) Avg =avg + nums[i]; Avg = avg/10; Console.WriteLine(“Average:” +avg); } }
Multi-dimensional Arrays Two-Dimensional Arrays Arrays of three or more Dimensions
Two-Dimensional Arrays Int [,] table = new int [10,20]; This creates a 10 * 20 array.
Two-Dimensional Arrays using System; Class twod { public static void main() { int t,i; int [,] table = new int[3,4]; For (t=0;t<3;++t) { For (i=0;i<4;++i) { Table [t,i]=(t*4)+i+1; Console.WriteLine (table [t,i]+ “ “); } Console.WriteLine(); }}}
Arrays of three or more Dimensions C# allows arrays with more than two dimensions. here is the general form of multidimensional array declaration: Type[,…,] name = new type [size1,size2,…,sizeN];
using System; Class threeDmatrix { public static void Main() { int[,,] m = new int [3,3,3]; int sum = 0; int n = 1; for (int x = 0;x<3;x++) for (int y = 0;y<3;y++) for (int z = 0;z<3;z++) M [x,y,z]=n++; sum = m[0,0,0] + m[1,1,1]+m[2,2,2]; Console.WriteLine(“sum of first diagonal:”+sum);}} Arrays of three Dimensions
Jagged Arrays Jagged arrays are declared by using sets of square brackets to indicate each dimension. For example, to declare a two-dimensional jagged array, you will use this general form: type [] [] array-name = new type [size][];
Jagged Arrays Int [][] jagged = new int [3] []; Jagged [0] = new int [3]; Jagged [1] = new int [2]; Jagged [2] = new int [4];
Jagged Arrays jagged [0][0], jagged [0][1], jagged[0][2] jagged [1][0], jagged [1][1] jagged [2][0], jagged [2][1], jagged[2][2], jagged [2][3]
Jagged Arrays 0 1 2 0 1 0 3 2 4