SlideShare ist ein Scribd-Unternehmen logo
1 von 13
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));

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Arrays
ArraysArrays
Arrays
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Java awt
Java awtJava awt
Java awt
 
array
array array
array
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Php array
Php arrayPhp array
Php array
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 

Andere mochten auch

Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
5 Array List, data structure course
5 Array List, data structure course5 Array List, data structure course
5 Array List, data structure courseMahmoud Alfarra
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Csc153 chapter 06
Csc153 chapter 06Csc153 chapter 06
Csc153 chapter 06PCC
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.CIIT Atd.
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming ConceptsLearning VB.NET Programming Concepts
Learning VB.NET Programming Conceptsguest25d6e3
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 

Andere mochten auch (20)

Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
5 Array List, data structure course
5 Array List, data structure course5 Array List, data structure course
5 Array List, data structure course
 
Array data structure
Array data structureArray data structure
Array data structure
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Array in c language
Array in c languageArray in c language
Array in c language
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Lecture 2c stacks
Lecture 2c stacksLecture 2c stacks
Lecture 2c stacks
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Array y Objects C#
Array y Objects C#Array y Objects C#
Array y Objects C#
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Csc153 chapter 06
Csc153 chapter 06Csc153 chapter 06
Csc153 chapter 06
 
C++ arrays part2
C++ arrays part2C++ arrays part2
C++ arrays part2
 
Arrays
ArraysArrays
Arrays
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming ConceptsLearning VB.NET Programming Concepts
Learning VB.NET Programming Concepts
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 

Ähnlich wie Arrays C#

Ähnlich wie Arrays C# (20)

Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
C# p9
C# p9C# p9
C# p9
 
Arrays
ArraysArrays
Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Arrays
ArraysArrays
Arrays
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
Array lecture
Array lectureArray lecture
Array lecture
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 

Mehr von Raghuveer Guthikonda (9)

C# String
C# StringC# String
C# String
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
 
Generics C#
Generics C#Generics C#
Generics C#
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Regex in C#
Regex in C#Regex in C#
Regex in C#
 

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));