Arrays C#

Click here to load reader

download Arrays C#

of 13

Transcript of Arrays C#

  • 1. ArraysSimple ArraysMultidimensional ArraysJagged ArraysThe Array classyield Statement

2. Simple Arrays An array is a data structure that contains a numberof elements of the same type. Array Declaration Syntax: datatype[] myArray; Array Initialization Syntax: datatype[] myArray=new datatype[size];// size is a integervalue 3. Simple Arrays With this declaration and initialization, the variablemyArray references four integer values that areallocated on the managed heap. 4. Multidimensional Arrays One dimensional arrays are indexed by a single integer,A multidimensional array is indexed by two or moreintegers. 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 rectangularsize(Ex: 3X3). A jagged array is more flexible in sizing the array,With jagged array every row can have differentSize. 6. Jagged Arrays A jagged array is an array whose elements are arrays.The elements of a jagged array can be of differentdimensions and sizes. A jagged array is sometimescalled 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, therebyserving as the base class for all arrays in the commonlanguage runtime. Declaring an array with brackets is C# notation usingArray Class. Creating Array using Array class Array arr1=Array.CreateInstance(typeof(datatype),size); Copying Arrays The Clone() method that is defined with ICloneable creates ashallow copy of the array. Array class implements the interface ICloneable. Ex: int[] arr1={1,2}; int [] arr2=(int[])arr1.Clone(); 8. IEnumerator Interface Supports a simple iteration over a generic collection. Syntax: public interface IEnumerator : IDisposable, IEnumerator out T The type of objects to enumerate. This type parameter is covariant. That is, you can use either the typeyou specified or any type that is more derived. Properties Current Gets the element in the collection at the current position ofthe enumerator. Methods Dispose Performs application-defined tasks associated with freeing,releasing, or resetting unmanaged resources. MoveNext Advances the enumerator to the next element of thecollection. Reset Sets the enumerator to its initial position, which is before thefirst 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 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 creatingenumerations easily. Used in an iterator block to provide a value to theenumerator object or to signal the end of iteration. Ittakes one of the following forms: yield return ; yield break; yield return statement returns one element of acollection and moves the position to the next element,and yield break stops the iteration. 11. TUPLES 12. Tuples The release of .NET Framework 4.0 adds Tuples tothe base class library. Tuples have the origin in functional programminglanguages like F#. .NET 4 defines eight Tuple classes and one staticTuple class that act as a factory of tuples. 13. Creating a Tuple Example 1:public static Tuple Divide(int divd, int divs){int res=divd/divs; int rem=divd%divs; return Tuple.Create(res, rem);}var res=Divide(5,2);Console.Write(res.Item1+t+res.Item2); Example 2:var tuple=Tuple.Create( Hello, Welcome To, KMIT,1,5,78); You can have Tuple type itself as parameter. Ex:var tuple=Tuple.Create>(Hello, Welcome To, KMIT,1,5,78,Tuple.Create(52,59));