Tuesday, May 5, 2009

Anonymous Methods in C#

protected void DemoForm_Load(object sender, EventArgs e)
{
List customerList = new List();
customerList.Add(new Customer(00004, "kabir"));
customerList.Add(new Customer(00005, "Krishan"));
customerList.Add(new Customer(00006, "Karan"));
customerList.Sort( delegate(Customer lhs, Customer rhs)
{
return lhs.CustomerID.CompareTo(rhs.CustomerID);
//OR return rhs.CustomerID.CompareTo(lhs.CustomerID);
}); //one can use customerList.Reverse() to reverse the sort order
foreach (Customer customer in customerList)
Response.Write(customer.CustomerID+"");
}
public class Customer
{
private int m_CustomerID = int.MinValue;
private string m_CustomerName = string.Empty;
public Customer(int customerId,string customerName)
{
this.m_CustomerID = customerId;
this.m_CustomerName = customerName;
}
public int CustomerID
{
get { return m_CustomerID;
}
set
{
m_CustomerID = value;
}
}
public string CustomerName
{
get
{
return m_CustomerName;
}
set
{
m_CustomerName = value;
}
}
}

No comments:

Post a Comment

 
Locations of visitors to this page