Add Item to a list object : List : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » List »

 

Add Item to a list object








    
 

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;


public class Customer : System.IComparable {
    private int _id;
    private string _name;
    private string _rating;
    private static SortOrder _order;

    public enum SortOrder {
        Ascending = 0,
        Descending = 1
    }

    public Customer(int id, string name)
        : this(id, name, "Other") {
    }

    public Customer(int id, string name, string rating) {
        this._id = id;
        this._name = name;
        this._rating = rating;
    }

    public int Id {
        get { return this._id; }
        set { this._id = value; }
    }

    public string Name {
        get { return this._name; }
        set { this._name = value; }
    }

    public string Rating {
        get { return this._rating; }
        set { this._rating = value; }
    }

    public static SortOrder Order {
        get { return _order; }
        set { _order = value; }
    }

    public override bool Equals(Object obj) {
        bool retVal = false;
        if (obj != null) {
            Customer custObj = (Customer)obj;
            if ((custObj.Id == this.Id) &&
                (custObj.Name.Equals(this.Name) &&
                (custObj.Rating.Equals(this.Rating))))
                retVal = true;
        }
        return retVal;
    }

    public override string ToString() {
        return this._id + ": " + this._name;
    }

    public int CompareTo(Object obj) {
        switch (_order) {
            case SortOrder.Ascending:
                return this.Name.CompareTo(((Customer)obj).Name);
            case SortOrder.Descending:
                return (((Customer)obj).Name).CompareTo(this.Name);
            default:
                return this.Name.CompareTo(((Customer)obj).Name);
        }
    }

}

public class CollectionTest {
    public static void Main() {
        Collection<Customer> collCustList = new Collection<Customer>();
        collCustList.Add(new Customer(99, "Happy Gillmore"));
        collCustList.Add(new Customer(77, "Billy Madison"));

        List<Customer> rangeList = new List<Customer>();
        rangeList.Add(new Customer(55, "Bobby Boucher"));
        rangeList.Add(new Customer(44, "Robbie Hart"));

        List<Customer> masterList = new List<Customer>(collCustList);
        masterList.AddRange(rangeList);

        masterList.Insert(2, new Customer(33, "Longfellow Deeds"));
        masterList[3] = new Customer(88, "Sonny Koufax");

        foreach (Customer cust in masterList)
            Console.Out.WriteLine(cust);
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» List