Friday, August 1, 2008

List<T>.foreach

Here is a sample code on how to use 'foreach' on 'generic list'
Notice the call to the anonymous function


class Employee
{
private string _nom;
private double _salary;

public Employee()
{
}

public Employee(string nom, double salary )
{
_nom = nom;
_salary = salary;

}

public string Nom
{
get {return _nom; }
set { _nom = value;}

}

public double Salary
{
get { return _salary;}
set { _salary = value; }
}
}



class Program
{

public static void incSalary(Employee p)
{
p.Salary += 100;
Console.WriteLine(p.Nom + Convert.ToString(p.Salary));
}

public static void Main(string[] args)
{
List lstEmployee = new List();

lstEmployee.Add(new Employee("Paul",3300));
lstEmployee.Add(new Employee("Raphael",2500));

lstEmployee.ForEach(incSalary);

lstEmployee.ForEach(delegate(Employee p){Console.WriteLine(p.Nom);});

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}

No comments:

Post a Comment