Arrays, Structures And Enums

20
Arrays, Structures, Enums Bhushan Mulmule [email protected] www.dotnetvideotutorial.com

description

Arrays, Multidimentional Arrays, Jagged Arrays, Structures and Enums

Transcript of Arrays, Structures And Enums

Page 1: Arrays, Structures And Enums

Arrays, Structures, EnumsBhushan Mulmule

bhushan.mulmule@dotnetvideotutorial.comwww.dotnetvideotutorial.com

Page 2: Arrays, Structures And Enums

For video visit www.dotnetvideotutorial.com

Page 3: Arrays, Structures And Enums

Agenda

Single Dimensional Array

Array Class

Array of Reference Type

Double Dimensional Array

Jagged Array

Structure

Enum

www.dotnetvideotutorial.com

Page 4: Arrays, Structures And Enums

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

Page 5: Arrays, Structures And Enums

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

Page 6: Arrays, Structures And Enums

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

Page 7: Arrays, Structures And Enums

Array ClassBase class for all arrays in the common language

runtime.

Provides methods for creating, manipulating, searching, and sorting arrays

www.dotnetvideotutorial.com

Page 8: Arrays, Structures And Enums

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

Page 9: Arrays, Structures And Enums

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

Page 10: Arrays, Structures And Enums

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

Page 11: Arrays, Structures And Enums

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

Page 12: Arrays, Structures And Enums

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

Page 13: Arrays, Structures And Enums

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

Page 14: Arrays, Structures And Enums

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

Page 15: Arrays, Structures And Enums

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.

Page 16: Arrays, Structures And Enums

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

Page 17: Arrays, Structures And Enums

Enum

enum Gender{ Male, Female, Other}

Gender g1 = Gender.Female;

1g1

www.dotnetvideotutorial.com

Page 18: Arrays, Structures And Enums

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

Page 19: Arrays, Structures And Enums

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

Page 20: Arrays, Structures And Enums

Bhushan [email protected]

www.dotnetvideotutorial.com