# The lambda expression in C # moving up another gear

The lambda expression in C # moving up another gear
by Ettougourti Mohamed Ali

You would be surprised to learn that we have done in our previous section touched easy aspects of using the lambda expression with vectors and arrays.
The « normal » use relates the well-known conventional vectors.
But what if we plan to use vectors of a particular type?

A vector class or structure defined by us?
Two examples: the first milking fractions vector,

the second relates to a vector of musical notes.

In both cases structure are defined by us.
Let’s start with the first example by creating together a special class which is called « fraction »

Class fraction {};

To be visible from all public we qualify the class as public.
Public class fraction {};
Two constructors are provided to initialize the class:
Public fraction (int d);
And
Public fraction (int n, int d);
The first constructor assumes the numerator of the fraction equal to 1.

The instruction
New fraction (2);
Gives a fraction of a half 1/2 ..

The second most explicit constructor requires and the numerator and the denominator.
The instruction
New fraction (2, 4);
Gives a fraction of 2/4 two quarters or a half.
Here is all the concocted code.

public class fraction

{
Public int num;
public int den;
public fraction (int d)
{
this.num = 1;
this.den = d;
}
public fraction (int n, int d)
{
this.num = n;
this.den = d;
}
}

We propose to create a vector of class fractions that we simply appoint x.

Fraction x = new fraction ;

Initializing the array with a series of fractions

x  = new fraction (1, 10);

x  = new fraction (1, 4);

x  = new fraction (1, 24);

x  = new fraction (1, 10);

x  = new fraction (1, 5);

The challenge we face is to use the lambda expression to process data of the vector x to find the maximum, the minimum and their respective positions for example.

The flood instruction

fraction max= x.Max ();

Will cause a compiler error because the fraction class contains no CompareTo () method that would allow comparison between the different elements of the vector. CompareTo method is necessary to find the maximum and the minimum values.
The compiler standing by its CompareTo method we must comply.
We resume therfore our code written for the « fraction » class by declaring the fraction base class IComparable.

public class fraction: IComparable
And then just adding a CompareTo method.

int CompareTo (object obj)

{

fraction f = (fraction) obj;

int n = f.num * den;

int d = f.den * den;

int num = n2 * f.den;

int d2 = den * f.den;

if (n == n2) return 0;

else if (n2> n) return 1;

return -1;

}

The CompareTo method begins by calculating the common denominator between the two fractions.

If both are equal numerators our method returns zero to say that both fractions are equal if n2 is greater than n the method returns 1 otherwise it returns -1;

Our fraction rewritten class now looks like the following code:

public class fraction: IComparable

{

public int num;

public int den;

public fraction (int d)

{

this.num = 1;

this.den = d;

}

public fraction (int n, int d)

{

this.num = n;

this.den = d;

}

public int CompareTo (object obj)

{

fraction f = (fraction) obj;

int n = f.num * den;

int d = f.den * den;

int num = n2 * f.den;

int d2 = den * f.den;

if (n == n2) return 0;

else if (n2> n) return 1;

return -1;

}

}

we can now safetly seek the greatest value of the vector x.

fraction max = x.MAX();

Or

fraction max = x.MAx (b => b);

Max takes the value of 1/4;

The instruction

Fraction min = x.Min();

Or

Fraction min= x.Min (b => b);

refers 1/24;

To sort the vector from the smallest fractions to the largest we can still use the lambda expression with instruction orderby

x = x.OrderBy (b => b) .ToArray ();

the vector x is now ordered as follows

1/24, 1/10, 1/10, 1/5, 1/4

The instruction

x = x.OrderByDescending (b => b) .ToArray ();

Operates a descending sort.

The reverse statement is also operational.

x = x.Reverse().ToArray ().

The instruction indexof already encountered works just as well.

int pos = x.ToList ().IndexOf (max.);

pos = x.ToList () .IndexOf (min.);

(19)