Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Generics are pretty cool. You can think of Generics as a basket which can hold different datatypes without the process of casting. Here are few examples of Generics in action: 

Custom Collection:

You can make use of the Generics to create a custom collection.

System.Collections.Generic.List<Users> users = new List<Users>();

        
for (int i = 0; i <= 9; i++)
        {
            Users user = 
new Users();
            user.FirstName = "Mohammad" + i;
            user.LastName = "Azam" + i;
            users.Add(user);
        }

        
// print the users from the collection
        
foreach (Users user in users)
        {
            Console.WriteLine(user.LastName);
            Console.WriteLine(user.FirstName);
        }        

Making Custom Generic Collection:

You can even make your own custom generic collection. Your collection must implement the IEnumerable interface. Check out my custom collection below:

public class AzamSharpList<T> : IEnumerable
{
    T[] items;
    
int m_Pointer = 0; 
    
    
public AzamSharpList()
        : 
this(100)
    {
    }

    
public AzamSharpList(int size)
    {
        items = 
new T[size];
    }

    
public void Add(T item)
    {
        items[m_Pointer] = item;
        m_Pointer++;

    }

    
public T[] GetAllItems()
    {
        
return items; 
    }

    
public T GetItemAt(int index)
    {
        
if (index > m_Pointer)
        {
            
throw new Exception("No item at that position");
        }

        
return items[index]; 
    }

   

#region IEnumerable Members
        
    
IEnumerator IEnumerable.GetEnumerator()
    {
        
throw new Exception("The method or operation is not implemented.");
    }

    
#endregion
}

Using the Custom Collection:

AzamSharpList<string> sharpList = new AzamSharpList<string>(10);
        sharpList.Add("AzamSharp");
        sharpList.Add("Sam");
        sharpList.Add("Mary");
        sharpList.Add("John Doe");

        
string[] myStringArray = sharpList.GetAllItems(); 

And also:

AzamSharpList<int> sharpIntList = new AzamSharpList<int>(10);
        sharpIntList.Add(10);
        sharpIntList.Add(23);
        sharpIntList.Add(12);

        
int[] myIntegerArray = sharpIntList.GetAllItems();

Implementing the Find Method:

You can even implement a Find method which takes the collection as the parameter and returns boolean indicating if the item is found in the collection or not.

 public static bool Find<T>(T[] myItems, T item)
    {
        
bool result = false;

        
foreach (T value in myItems)
        {
            
if (value != null)
            {
                
if (value.Equals(item))
                {
                    result = 
true;
                    
break;
                }
                
else { result = false; }         
              
            }
            
        }

        
return result; 

    }

And you can use it like this:

Find<string>(sharpList.GetAllItems(), "AzamSharp");  
Console.WriteLine(Find<
int>(sharpIntList.GetAllItems(), 99));

 

 


 


 

  

 

powered by IMHO

 

Posted on Monday, October 31, 2005 10:20 AM | Back to top


Comments on this post: Generics in C# 2.0

# re: Generics in C# 2.0
Requesting Gravatar...
Like templates from C++ :) ????
Left by Kashif Khan on Nov 03, 2005 5:10 PM

# re: Generics in C# 2.0
Requesting Gravatar...

Yes, you are correct. These are like templates in C++. :)

Left by Azamsharp on Nov 03, 2005 5:13 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net