Change Contents : List « Collections Data Structure « C# / C Sharp






Change Contents

     

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() {
        List<Customer> collCustList = new List<Customer>();
        collCustList.Add(new Customer(99, "H", "P"));
        collCustList.Add(new Customer(77, "B", "G"));
        collCustList.Add(new Customer(55, "B", "G"));
        collCustList.Add(new Customer(88, "B", "P"));
        collCustList.Add(new Customer(11, "L", "O"));

        List<string> custNames = collCustList.ConvertAll<string>(delegate(Customer cust) {
            string retVal = cust.Name;
            if (cust.Rating.Equals("P"))
                retVal = cust.Name + " ****";
            else if (cust.Rating.Equals("G"))
                retVal = cust.Name + " ***";
            else if (cust.Rating.Equals("O"))
                retVal = cust.Name + " **";
            return retVal;
        });

        foreach (string custName in custNames)
            Console.Out.WriteLine(custName);

        string[] deepNameCopy = new string[custNames.Count];
        custNames.CopyTo(deepNameCopy);
        custNames[0] = "CHANGED NAME";

        foreach (String custName in custNames)
            Console.Out.WriteLine(custName);

        foreach (string custName in deepNameCopy)
            Console.Out.WriteLine(custName);

        collCustList.ForEach(delegate(Customer cust) {
            cust.Name = cust.Name.ToUpper();
        });

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

   
    
    
    
  








Related examples in the same category

1.new List() Add(assembly1) Indexer
2.Get the size of a list
3.Add Item to a list object
4.Search Test
5.Remove Item from a List
6.Reverse Contents
7.Hash List to Hash Array
8.Load elements into a generic list from an array
9.Lazy List
10.Slice list
11.Chunked List
12.Flatten a List
13.Keyed List
14.Convert a collection of strings to a comma separated list.
15.Convert List To Array
16.Compare two Lists
17.Name Value Pair List
18.Get Distinct value from a List
19.Adds the elements of the specified collection to the specified generic IList.
20.List(T) Class represents a strongly typed list of objects that can be accessed by index.
21.IList Interface represents a non-generic collection of objects that can be individually accessed by index.
22.Searches a range of elements in the sorted List for an element using the specified comparer and returns the zero-based index of the element.
23.Determines whether an element is in the List.
24.Converts the elements in the current List to another type, and returns a list containing the converted elements.
25.Copies a range of elements from the List to a compatible one-dimensional array, starting at the specified index of the target array.
26.Determines whether the List contains elements that match the conditions defined by the specified predicate.
27.Performs the specified action on each element of the List.
28.Searches for object and returns the zero-based index
29.Searches for object and returns the zero-based index of the last occurrence
30.Reverses the order of the elements in the specified range.
31.Weak Collection
32.Sorted Collection
33.Paged List
34.Get Search List
35.Find All Index
36.Evented List
37.SortedList
38.Reverse a List
39.Shuffles the specified list
40.Sequence is an abstraction of a data reseqeuncer
41.Generic Paged List
42.A list implementation that is loaded the first the contents are examined
43.Creates a list by combining two other lists into one.
44.Creates a list by repeating another list.
45.Compare two arrays and two lists