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