For those of you creating applications in .NET 2.0, I hope that you’re taking advantage of generics.  In particular, as the title of this post suggests, I hope you’re using constraints within your methods.  Why?  Well, with contraints in your methods you can do things such as this,

namespace GenericMethodConstraints
{
    
using System;
    using 
System.Collections.Generic;

    class 
Program
    {
        
static void Main(string[] args)
        {
            
// Create the list of items based on the parameters
            
List<Test> items Driver.CreateList<Test>(10);

            
// Loop through them and output their values
            
foreach (Test t in items)
            {
                Console.WriteLine(
"{0} - {1}", t.ID.ToString(), t.Name);
            
}

            Console.Write(
"Press <ENTER> to exit.");
            
Console.ReadLine();
        
}
    }

    
class Driver
    {
        
public static List<TInterface> CreateList<TInterface>(int itemCount) where TInterface : ITest, new()
        {
            
// Create a list of items
            
List<TInterface> items = new List<TInterface>(itemCount);

            for 
(int 0i < itemCounti++)
            {
                
// Check this out!  I'm treating the interface as an object
                // because it has the new() constraint.
                
TInterface iface = new TInterface();

                
// Set the properties for the instance
                
iface.ID i;
                
iface.Name i.ToString();

                
items.Add(iface);
            
}

            
return items;
        
}
    }

    
/// <summary>
    /// Interface needed for the constraint
    /// </summary>
    
interface ITest
    {
        
int ID { get; set; }
        
string Name { get; set;}
    }

    
/// <summary>
    /// Class that implements the interface for the constraint
    /// </summary>
    
class Test : ITest
    {
        
private int id;
        private string 
name;

        public 
Test() { }

        
public int ID
        {
            
get return id}
            
set { id = value; }
        }

        
public string Name
        {
            
get return name}
            
set { name = value; }
        }
    }
}

As you can see, you can set contraints on your methods that specify that your generic type is of a specific interface and that the class defines a constructor.  This will allow you to create a type that implements your interface within that method.  And you’re guaranteed that it will work since the constraints will be enforced at compile time!  For those of you that want more proof, try setting the contraint of TInterface from ITest to System.Data.IDbParameter and modify the body to populate the correct properties.