SlideShare a Scribd company logo
Arrays

Simple Arrays
Multidimensional Arrays
Jagged Arrays
The Array class
yield Statement
Simple Arrays
 An array is a data structure that contains a number
  of elements of the same type.
 Array Declaration
   Syntax:
     datatype[] myArray;

 Array Initialization
   Syntax:
     datatype[] myArray=new datatype[size];// size is a integer
      value
Simple Arrays
 With this declaration and initialization, the variable
  myArray references four integer values that are
  allocated on the managed heap.
Multidimensional Arrays
 One dimensional arrays are indexed by a single integer,
  A multidimensional array is indexed by two or more
  integers.
 Syntax:
   datatype[,] twodim=new datatype[size1,size2];
 Ex:
   int [,] twodim={{1,2,3},{4,5,6},{7,8,9}};
Jagged Arrays
 A two-dimensional array has a rectangular
  size(Ex: 3X3).
 A jagged array is more flexible in sizing the array,
  With jagged array every row can have different
  Size.
Jagged Arrays
 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."
 Syntax:
     datatype[][] jagged =new datatype[size][];
     Jagged[0]=new datatype[2]{//values};
     Jagged[1]=new datatype[6]{//values};
     Jagged[2]=new datatype[4]{//values};
     ..
The Array Class
 Array Class provides methods for creating,
  manipulating, searching, and sorting arrays, thereby
  serving as the base class for all arrays in the common
  language runtime.
 Declaring an array with brackets is C# notation using
  Array Class.
 Creating Array using Array class
     Array arr1=Array.CreateInstance(typeof(datatype),size);

 Copying Arrays
     The Clone() method that is defined with ICloneable creates a
      shallow copy of the array.
     Array class implements the interface ICloneable.
     Ex:
       int[] arr1={1,2};
       int [] arr2=(int[])arr1.Clone();
IEnumerator<T> Interface
 Supports a simple iteration over a generic collection.
    Syntax:
      public interface IEnumerator<out T> : IDisposable, IEnumerator
    out T
      The type of objects to enumerate.
      This type parameter is covariant. That is, you can use either the type
        you specified or any type that is more derived.
 Properties
     Current  Gets the element in the collection at the current position of
      the enumerator.
 Methods
     Dispose Performs application-defined tasks associated with freeing,
      releasing, or resetting unmanaged resources.
     MoveNext Advances the enumerator to the next element of the
      collection.
     Reset Sets the enumerator to its initial position, which is before the
      first element in the collection.
foreach Statement
 The foreach statement repeats a group of embedded
 statements for each element in an array or an object
 collection that implements the
 System.Collections.IEnumerable or
 System.Collections.Generic.IEnumerable<T> interface.
     int[] array1 = new int[] { 0, 1, 2, 3, 5, 8, 13 };
     foreach (int i in array1)
     {
     System.Console.WriteLine(i);
     }
yield Statement
 C# 2.0 added the yield statement for creating
  enumerations easily.
 Used in an iterator block to provide a value to the
  enumerator object or to signal the end of iteration. It
  takes one of the following forms:
       yield return <expression>;
       yield break;
 yield return statement returns one element of a
  collection and moves the position to the next element,
  and yield break stops the iteration.
TUPLES
Tuples
 The release of .NET Framework 4.0 adds Tuples to
  the base class library.
 Tuples have the origin in functional programming
  languages like F#.
 .NET 4 defines eight Tuple classes and one static
  Tuple class that act as a factory of tuples.
Creating a Tuple
 Example 1:
    public static Tuple<int,int> Divide(int divd, int divs)
    {int res=divd/divs;
         int rem=divd%divs;
         return Tuple.Create<int, int>(res, rem);
    }
    var res=Divide(5,2);
    Console.Write(res.Item1+”t”+res.Item2);
   Example 2:
    var tuple=Tuple.Create<string, string, string, int, int, int>(
                       “Hello”, ”Welcome To”, ”KMIT”,1,5,78);
 You can have Tuple type itself as parameter.
   Ex:
    var tuple=Tuple.Create<string, string, string, int, int, int,Tuple<int,int>>
                (“Hello”, ”Welcome To”, ”KMIT”,1,5,78,Tuple.Create<int,
                           int>(52,59));

More Related Content

PPTX
C# classes objects
PDF
Php array
PPTX
Classes, objects in JAVA
PPTX
Strings in Java
PPT
Java interfaces
PPTX
PPTX
Array in c#
C# classes objects
Php array
Classes, objects in JAVA
Strings in Java
Java interfaces
Array in c#

What's hot (20)

PPTX
Delegates and events in C#
PPTX
Java swing
PPT
Java Streams
PPT
C# basics
PPTX
PPTX
Templates in c++
PPTX
Arrays in Java
PPTX
Operators in java
PPT
Looping statements in Java
PPT
standard template library(STL) in C++
PDF
Arrays in python
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Templates in C++
PPTX
Java exception handling
PPTX
This keyword in java
PPTX
JAVA AWT
PPT
Jdbc ppt
PPTX
C# Arrays
PPTX
Operators in java
Delegates and events in C#
Java swing
Java Streams
C# basics
Templates in c++
Arrays in Java
Operators in java
Looping statements in Java
standard template library(STL) in C++
Arrays in python
Basic Concepts of OOPs (Object Oriented Programming in Java)
Templates in C++
Java exception handling
This keyword in java
JAVA AWT
Jdbc ppt
C# Arrays
Operators in java
Ad

Viewers also liked (20)

PPT
PDF
Lecture17 arrays.ppt
PPTX
5 Array List, data structure course
PDF
Array data structure
PPT
Arrays Data Structure
PPTX
Array in C# 3.5
PPTX
Array in c language
PPT
DATA STRUCTURES
DOC
Arrays In General
PPT
Lecture 2c stacks
PPSX
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
PPTX
Array y Objects C#
PPSX
Lecture 2 data structures & algorithms - sorting techniques
PPTX
Lecture 3 data structures and algorithms
PPT
Csc153 chapter 06
 
PDF
C++ arrays part2
PPTX
PPT
Lecture 2a arrays
PPT
stack and queue array implementation in java.
PDF
Learning VB.NET Programming Concepts
Lecture17 arrays.ppt
5 Array List, data structure course
Array data structure
Arrays Data Structure
Array in C# 3.5
Array in c language
DATA STRUCTURES
Arrays In General
Lecture 2c stacks
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Array y Objects C#
Lecture 2 data structures & algorithms - sorting techniques
Lecture 3 data structures and algorithms
Csc153 chapter 06
 
C++ arrays part2
Lecture 2a arrays
stack and queue array implementation in java.
Learning VB.NET Programming Concepts
Ad

Similar to Arrays C# (20)

PPTX
Intro to C# - part 2.pptx emerging technology
PDF
PDF
Arrays
PPT
Data Structure Midterm Lesson Arrays
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Computer programming 2 Lesson 13
PPTX
Arrays in programming
PPT
Java: Introduction to Arrays
PPTX
Lecture 9
PDF
C sharp chap6
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPT
Generics collections
PPTX
Array lecture
PPT
Generics Collections
PPTX
Chapter 7.1
PPTX
Arrays in Java with example and types of array.pptx
PPTX
Arrays in Data Structure and Algorithm
PDF
ARRAYS
PPTX
Array Introduction One-dimensional array Multidimensional array
Intro to C# - part 2.pptx emerging technology
Arrays
Data Structure Midterm Lesson Arrays
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Computer programming 2 Lesson 13
Arrays in programming
Java: Introduction to Arrays
Lecture 9
C sharp chap6
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Generics collections
Array lecture
Generics Collections
Chapter 7.1
Arrays in Java with example and types of array.pptx
Arrays in Data Structure and Algorithm
ARRAYS
Array Introduction One-dimensional array Multidimensional array

More from Raghuveer Guthikonda (9)

PPTX
PPTX
C# Delegates
PPTX
Operators & Casts
PPTX
PPT
Inheritance C#
PPTX
Objects and Types C#
PPTX
Introduction to C#
PPT
Introduction to .NET Framework
PPTX
C# Delegates
Operators & Casts
Inheritance C#
Objects and Types C#
Introduction to C#
Introduction to .NET Framework

Arrays C#

  • 1. Arrays Simple Arrays Multidimensional Arrays Jagged Arrays The Array class yield Statement
  • 2. Simple Arrays  An array is a data structure that contains a number of elements of the same type.  Array Declaration  Syntax:  datatype[] myArray;  Array Initialization  Syntax:  datatype[] myArray=new datatype[size];// size is a integer value
  • 3. Simple Arrays  With this declaration and initialization, the variable myArray references four integer values that are allocated on the managed heap.
  • 4. Multidimensional Arrays  One dimensional arrays are indexed by a single integer, A multidimensional array is indexed by two or more integers.  Syntax:  datatype[,] twodim=new datatype[size1,size2];  Ex:  int [,] twodim={{1,2,3},{4,5,6},{7,8,9}};
  • 5. Jagged Arrays  A two-dimensional array has a rectangular size(Ex: 3X3).  A jagged array is more flexible in sizing the array, With jagged array every row can have different Size.
  • 6. Jagged Arrays  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."  Syntax:  datatype[][] jagged =new datatype[size][];  Jagged[0]=new datatype[2]{//values};  Jagged[1]=new datatype[6]{//values};  Jagged[2]=new datatype[4]{//values};  ..
  • 7. The Array Class  Array Class provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.  Declaring an array with brackets is C# notation using Array Class.  Creating Array using Array class  Array arr1=Array.CreateInstance(typeof(datatype),size);  Copying Arrays  The Clone() method that is defined with ICloneable creates a shallow copy of the array.  Array class implements the interface ICloneable.  Ex:  int[] arr1={1,2};  int [] arr2=(int[])arr1.Clone();
  • 8. IEnumerator<T> Interface  Supports a simple iteration over a generic collection.  Syntax:  public interface IEnumerator<out T> : IDisposable, IEnumerator  out T  The type of objects to enumerate.  This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived.  Properties  Current  Gets the element in the collection at the current position of the enumerator.  Methods  Dispose Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.  MoveNext Advances the enumerator to the next element of the collection.  Reset Sets the enumerator to its initial position, which is before the first element in the collection.
  • 9. foreach Statement  The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. int[] array1 = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in array1) { System.Console.WriteLine(i); }
  • 10. yield Statement  C# 2.0 added the yield statement for creating enumerations easily.  Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration. It takes one of the following forms: yield return <expression>; yield break;  yield return statement returns one element of a collection and moves the position to the next element, and yield break stops the iteration.
  • 12. Tuples  The release of .NET Framework 4.0 adds Tuples to the base class library.  Tuples have the origin in functional programming languages like F#.  .NET 4 defines eight Tuple classes and one static Tuple class that act as a factory of tuples.
  • 13. Creating a Tuple  Example 1: public static Tuple<int,int> Divide(int divd, int divs) {int res=divd/divs; int rem=divd%divs; return Tuple.Create<int, int>(res, rem); } var res=Divide(5,2); Console.Write(res.Item1+”t”+res.Item2);  Example 2: var tuple=Tuple.Create<string, string, string, int, int, int>( “Hello”, ”Welcome To”, ”KMIT”,1,5,78);  You can have Tuple type itself as parameter.  Ex: var tuple=Tuple.Create<string, string, string, int, int, int,Tuple<int,int>> (“Hello”, ”Welcome To”, ”KMIT”,1,5,78,Tuple.Create<int, int>(52,59));