• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Arrays, Structures And Enums
 

Arrays, Structures And Enums

on

  • 371 views

Arrays, Multidimentional Arrays, Jagged Arrays, Structures and Enums

Arrays, Multidimentional Arrays, Jagged Arrays, Structures and Enums

Statistics

Views

Total Views
371
Views on SlideShare
371
Embed Views
0

Actions

Likes
0
Downloads
73
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Arrays, Structures And Enums Arrays, Structures And Enums Presentation Transcript

    • Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.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 Array 20 50 60 0 1 2 Arrays are zero indexed Last index is always (size – 1) 5000 5000 marks 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]; 5000 5000 array1 0 1 2 3 4 www.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 5000 marks The default value for elements of numeric array is zero 0 1 2 Single Dimensional Array www.dotnetvideotutorial.com
    • Array Class  Base class for all arrays in the common language runtime.  Provides methods for creating, manipulating, searching, and sorting arrays www.dotnetvideotutorial.com
    • Array Class int[] numbers = { 78, 54, 76, 23, 87 }; //sorting Array.Sort(numbers); Console.WriteLine("sorted array: "); for (int i = 0; i < numbers.Length; i++) Console.WriteLine(numbers[i]); //reversing Array.Reverse(numbers); ... //finding out index int 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); } } 2000 2200 1800 Titan Mira K Computer 2000 1800 2200 5000 5000 Computers 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 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}:tt", 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 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
    • Jagged Array  Array 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 Array int[][] 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 }; 2000 1800 2200 5000 5000 a 2000 2200 1800 32 54 54 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(); } 2000 1800 2200 5000 5000 a 2000 2200 1800 32 54 54 76 23 78 96 46 38
    • Struct  Keyword 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; 1 g1 www.dotnetvideotutorial.com
    • Enums  The 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 Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com