Tuesday, May 08, 2007

A Custom Generic Collection which has List as well as Dictionary functionality for Custom Entity Classes

I frequently need a collection which List as well as Dictionary functionality for custom entity classes. So I wrote following generic class which provides the same functionality. There is one prerequisite for this class: Custom entity class should have override function ToString() which actually returns a unique key for this collection. Following is the code for Custom class:

using System;

using System.Collections.Generic;

public
class
CC<T> : IEnumerable<T>

{


public
List<T> List = new
List<T>();


public
Dictionary<string, T> Dict = new
Dictionary<string, T>();


public
void Add(T obj)

{

List.Add(obj);

Dict.Add(obj.ToString(), obj);

}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{


return PRIVGetEnumerator();

}


IEnumerator<T> IEnumerable<T>.GetEnumerator()

{


return PRIVGetEnumerator();

}



private
IEnumerator<T> PRIVGetEnumerator()

{


foreach (T obj in List)


yield
return obj;

}



public
bool Contains(String key)

{


return (Dict.ContainsKey(key));

}



public
void Remove(String key)

{

Dict.Remove(key);


for (int i = 0; i < List.Count; i++)

{


if (List[i].ToString().Equals(key))

{

List.RemoveAt(i);


break;

}

}

}



public
void RemoveAt(int index)

{

Dict.Remove(List[index].ToString());

List.RemoveAt(index);

}



public T this[int index]

{


get

{


return
this.List[index];

}


set

{

List[index] = value;


if (!Dict.ContainsKey(value.ToString()))

Dict.Add(value.ToString(), value);

}

}



public T this[string key]

{


get

{


return Dict[key];

}


set

{


if (!Dict.ContainsKey(key))

{

Dict[key] = value;

List.Add(value);

}


}

}



public
void CopyTo(T[] array, int index)

{

List.CopyTo(array, index);

}



public
void AddRange(List<T> value)

{


for (int i = 0; (i < value.Count); i = (i + 1))

{


this.Add(value[i]);

}

}



public
Dictionary<string, T>.KeyCollection Keys

{


get

{


return (Dict.Keys);

}

}



public
Dictionary<string, T>.ValueCollection Values

{


get

{


return (Dict.Values);

}

}



}


Following is the example of custom entity class:

using System;

using System.Xml.Serialization;

public
class
Person

{


private
string _id = "";


///
<summary>


/// Set or Get _id


///
</summary>

[XmlElement(ElementName = "Id")]


public
string Id

{


set { this._id = value; }


get { return
this._id; }

}



private
string _FirstName = "";


///
<summary>


/// Set or Get _FirstName


///
</summary>

[XmlElement(ElementName = "FirstName")]


public
string FirstName

{


set { this._FirstName = value; }


get { return
this._FirstName; }

}



private
string _LastName = "";


///
<summary>


/// Set or Get _LastName


///
</summary>

[XmlElement(ElementName = "LastName")]


public
string LastName

{


set { this._LastName = value; }


get { return
this._LastName; }

}



public
override
string ToString()

{


return
this.Id;

}


public Person(string id, string fName, string lName)

{


this.Id = id;


this.FirstName = fName;


this.LastName = lName;

}


}

Finally, following is the code snippet which shows how we can use this custom generic class:

class
Program

{


static
void Main(string[] args)

{



CC<Person> myCC = new
CC<Person>();

myCC.Add(new
Person("1", "Tom", "Hanks"));

myCC.Add(new
Person("2", "Julia", "Roberts"));

myCC.Add(new
Person("3", "Johny", "Depp"));


myCC[1] = new
Person("2", "Orlando", "Bloom");

myCC["4"] = new
Person("4", "Leonardo", "Dacaprio");

myCC.RemoveAt(0);



foreach (Person p in myCC)


Console.WriteLine(p.FirstName);


Console.ReadLine();

}

}


Happy Movies!!!


No comments: