.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Lists using Delegates

Posted By:Sasi Prabhu       Posted Date: July 30, 2011    Points: 40    Category: OOPS    URL: http://www.dotnetspark.com  

Add,Modify and Delete the Lists using Delegates.
 



class Program
{
    static void Main(string[] args)
    {            
        List Persons = new List();
 
        Persons.Add(new Person("SAM", 12));
        Persons.Add(new Person("RAJ", 7));
        Persons.Add(new Person("GURU", 16));
        Persons.Add(new Person("ARUN", 12));
        Persons.Add(new Person("GIRI", 45));
 
 
        Persons.RemoveAll(delegate(Person P)
        {
            return (P.Age < 20);
        });
 
 
        List NewList = Persons.FindAll(delegate(Person P)
        {
            return (P.Age > 25);
        });
 
        Persons.ForEach(delegate(Person P){
            P.Name = "<" + P.Name + ">";
        });
 

        bool Contains = Persons.Exists(delegate(Person P)
        {
            return (P.Age > 25);
        });
 
 

        foreach (Person PersonObject in NewList)
            Console.WriteLine(PersonObject);
 
    }
}
 

public class Person
{
    private string name = "";
    public string Name
    {
        get { return name;}
        set { name = value;}
    }
 
    private int age = 0;
    public int Age
    {
        get { return age; }
        set { age = value; }
    }	
 
    public Person(string Name, int Age)
    {
        this.name   = Name;
        this.age    = Age;
    }
 
    public override string ToString()
    {
        return String.Format("{0} ({1})", this.Name, this.Age);
    }
 
}




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend