Arrays, Structures And Enums
-
Upload
bhushan-mulmule -
Category
Technology
-
view
454 -
download
1
description
Transcript of Arrays, Structures And Enums
Arrays, Structures, EnumsBhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.comwww.dotnetvideotutorial.com
For video visit www.dotnetvideotutorial.com
Agenda
Single Dimensional Array
Array Class
Array of Reference Type
Double Dimensional Array
Jagged Array
Structure
Enum
www.dotnetvideotutorial.com
Array
Data structure holding multiple values of same data-
type
Are reference type
Derived from abstract base class Array20 50 60
0 1 2
Arrays are zero indexed
Last index is always (size – 1)
5000
5000marks
www.dotnetvideotutorial.com
Array Declarations
// Declare a single-dimensional array int[] array1 = new int[5];
// Declare and set array element values, Size can be skipped int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax. new keyword can be skipped int[] array3 = { 1, 3, 5, 7, 9 };
// Declaration and instantiation on separate lines int[] array4;array4 = new int[5];
50005000
array1
0 1 2 3 4www.dotnetvideotutorial.com
0 0
0
int[] marks = new int[3];Console.WriteLine("Enter marks in three subjects:");for (int i = 0; i < 3; i++) marks[i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Marks obtained:");Console.WriteLine("Printed using for loop");for (int i = 0; i < 3; i++) Console.WriteLine(marks[i]);
Console.WriteLine("Printed using foreach loop");foreach (int m in marks){ Console.WriteLine(m);}
20 50 60
5000
5000marks
The default value for elements of numeric
array is zero
0 1 2
Single Dimensional Array
www.dotnetvideotutorial.com
Array ClassBase class for all arrays in the common language
runtime.
Provides methods for creating, manipulating, searching, and sorting arrays
www.dotnetvideotutorial.com
Array Classint[] numbers = { 78, 54, 76, 23, 87 }; //sortingArray.Sort(numbers);Console.WriteLine("sorted array: ");for (int i = 0; i < numbers.Length; i++) Console.WriteLine(numbers[i]); //reversingArray.Reverse(numbers);...//finding out indexint index = Array.IndexOf(numbers, 54);Console.WriteLine("Index of 54: " + index);
www.dotnetvideotutorial.com
Array of Reference Types
static void Main(string[] args){ string[] computers = { "Titan", "Mira", "K computer" };
Console.WriteLine("Fastest Super Computers: \n"); foreach (string n in computers) { Console.WriteLine(n); }} 200
0220
0180
0
Titan
Mira
K Computer2000 1800
22005000
5000Computers
Note: Default value for elements of reference array
is null
int[,] marks = new int[3, 4];for (int i = 0; i < 3; i++){ C.WL("Enter marks in 4 subjects of Student {0}", i + 1); for (int j = 0; j < 4; j++) { marks[i, j] = Convert.ToInt32(Console.ReadLine()); }}
0 0 0
0 0 0
0 0 0
40 50 60 70
80 90 70 80
30 50 80 70
0
1
2
0 1 2 3
2D Array
www.dotnetvideotutorial.com
Console.WriteLine("---------------Score Board------------------");for (int i = 0; i < 3; i++){ Console.Write("Student {0}:\t\t", i + 1); for (int j = 0; j < 4; j++) { Console.Write(marks[i, j] + "\t"); } Console.WriteLine();}
0 0 0
0 0 0
0 0 0
40 50 60
70 80 90
70 80 30
0
1
2
0 1 2 3
2D Array
www.dotnetvideotutorial.com
Jagged ArrayArray of Arrays: A jagged array is an array whose
elements are arrays.
The elements of a jagged array can be of different dimensions and sizes.
Syntax:type[][] identifier = new type[size][];
www.dotnetvideotutorial.com
Jagged Arrayint[][] a = new int[3][];a[0] = new int[2] { 32, 54 };a[1] = new int[4] { 78, 96, 46, 38 };a[2] = new int[3] { 54, 76, 23 };
20001800
22005000
5000
a200
0220
0180
0
32 5454 76
23
78 96 46 38
for (int i = 0; i < 3; i++){
for (int j = 0; j < a[i].Length; j++){
Console.Write(a[i][j] + "\t");}Console.WriteLine();
}
20001800
22005000
5000
a2000 2200
1800
32 5454 76
23
78 96 46 38
StructKeyword to create user defined datatype
Typically used to encapsulate small groups of related variables
A struct type is a value type
Structs can implement an interface but can't inherit from
another struct or class.
NOTE: structs can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, consider making your type a class instead.
struct Point{ public int x; public int y;} class Program{ static void Main(string[] args) { Point p1; Console.WriteLine("Enter X and Y axis of point:"); p1.x = Convert.ToInt32(Console.ReadLine()); p1.y = Convert.ToInt32(Console.ReadLine()); C.WL("Coordinates of p1: x = {0}, y = {1}", p1.x, p1.y); Console.ReadKey(); }}
0 0
p1
x y
10 20
www.dotnetvideotutorial.com
Enum
enum Gender{ Male, Female, Other}
Gender g1 = Gender.Female;
1g1
www.dotnetvideotutorial.com
EnumsThe enum keyword is used to declare an enumeration,
a distinct type consisting of a set of named constants
called the enumerator list.
The default underlying type of the enumeration
elements is int.
By default, the first enumerator has the value 0, and the
value of each successive enumerator is increased by 1.
www.dotnetvideotutorial.com
enum Gender{ Male = 10, Female, Other}class Program{ static void Main(string[] args) { Gender g1 = Gender.Female; C.WL("g1 represents {0}. But value stored is {1}", g1, (int)g1);
Gender g2 = Gender.Male; C.WL("g2 represents {0}. But value stored is {1}", g2, (int)g2); }}
11
10
g1
g2
www.dotnetvideotutorial.com
Bhushan [email protected]
www.dotnetvideotutorial.com