Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts

Mar 30, 2015

Generic vs Non-Generic Collection in C#

Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type.
So it includes overhead of type conversions.

Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Some advantages of generic collections - Type Safe, Secure, reduced overhead of type conversions.

There are two distinct collection types in C#.

The standard collections, which are found under the System.Collections namespace and the generic collections, under System.Collections.Generic. Generic collections are more flexible and are the preferred way to work with data. The generic collections or generics were introduced in .NET framework 2.0. Generics enhance code reuse, type safety, and performance. 


Mar 10, 2015

Hashtable vs Dictionary



1. Hashtable is threadsafe and while Dictionary is not.
2. Dictionary is types means that the values need not to boxing while Hashtable
    values  need to be boxed or unboxed because it stored the  values and keys as
    objects.        
3. When you try to get the value of key which does not exists in the collection, the
    dictionary throws an exception of 'KeyNotFoundException' while hashtable returns
    null value.
4. When using large collection of key value pairs hashtable would be considered more
    efficient than dictionary.
5. When we retrieve the record in collection the hashtable does not maintain the order
    of entries while dictionary maintains the order of entries by which entries were added.


6. Dictionary relies on chaining whereas Hashtable relies on rehashing.


Dictionary:


  • It returns error if we try to find a key which does not exist.
  • It is faster than a Hashtable because there is no boxing and unboxing.
  • Only public static members are thread safe.
  • Dictionary is a generic type which means we can use it with any data type.

Hashtable:


  • It returns null if we try to find a key which does not exist.
  • It is slower than dictionary because it requires boxing and unboxing.
  • All the members in a Hashtable are thread safe,
  • Hashtable is not a generic type,

Jul 4, 2013

How to bind a List to an ASP.Net DropDownList

This code shows how you can build a custom class (Person for the demo), and then take a System.Collections.Generic.List, fill it with Person's and bind it to an ASP.Net DropDownList control.


First we need a Person class. I need to override the ToString() function if we want it to bind automatically without setting the DataTextField of the DropDownList. Also note that if we do want to set the DataTextField and DataValueField, then we have to have public properties. The following class will work either way we choose to bind it to the DropDownList.
 

public class Person
{
    private int _ID;
    private string _Name;
    private string _Color;
    public override string ToString()
    {
        return _Name;
    }
    // Properties   
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    public string Color
    {
        get { return _Color; }
        set { _Color = value; }
    }   
}
Ok so now we need a function in our ASP.net page that loads a <List> of Person's into the DropDownList.

Option 1) We set the DataTextField and the DataValueField. This relies on the public properties in Person

    protected void LoadList()
    {       
        List<Person> myPList = new List<Person>();

        Person p1 = new Person();
        p1.ID = 1;
        p1.Name = "Bob";
        p1.Color = "Blue";

        Person p2 = new Person();
        p2.ID = 2;
        p2.Name = "Joe";
        p2.Color = "Green";

        myPList.Add(p1);
        myPList.Add(p2);

        this.DropDownList1.DataSource = myPList;
        this.DropDownList1.DataTextField = "Color";
        this.DropDownList1.DataValueField = "ID";
        this.DropDownList1.DataBind();       
    }
The HTML output will be:
 <select name="DropDownList1" id="DropDownList1">
 <option value="1">Blue</option>
 <option value="2">Green</option>
 </select>



Option 2) Not setting the DataTextField and the DataValueField. This relies on the 'ToString() override in Person bind the text field.

protected void LoadList()
    {       
        List<Person> myPList = new List<Person>();

        Person p1 = new Person();
        p1.ID = 1;
        p1.Name = "Bob";
        p1.Color = "Blue";

        Person p2 = new Person();
        p2.ID = 2;
        p2.Name = "Joe";
        p2.Color = "Green";

        myPList.Add(p1);
        myPList.Add(p2);

        this.DropDownList1.DataSource = myPList;       
        this.DropDownList1.DataBind();       
    }

The HTML output will be the ToString() value for both the text and value.
<select name="DropDownList1" id="DropDownList1">
 <option value="Bob">Bob</option>
 <option value="Joe">Joe</option>
</select>