Generic in C# – Basic Practice Example

Generic is a feature of C# programming language, which helps us save a lot of repeating works in our journey. It is efficient and widely used in practice.

Background

Imaging, we need to add many items into a list; and these items might be String, Integer, etc. Without Generic, we need to write at least one function for each data type; this work can be repeated and boring.
With the benefit of Generic, we need to write the function once, instead of writing them all.

Example – add string items to a list

public class MyString
     {
         private List myStringList = new List();
         public void AddString(String item)     
         {         
             myStringList.Add(item);     
         }     
         public String GetStringAt(int i)     
         {         
             return myStringList[i];     
         } 

}

Example – add int items to a list

public class MyInteger 
{     
      private List<int> myIntList = new List<int>();     
      public void AddInt(int item)     
      {         
        myIntList.Add(item);     
      }     
      public int GetIntAt(int i)     
     {         
        return myIntList[i];     
     } 
}

As I mentioned before, we have to make an individual function for a different data type. There are many types out there, and they are countless types made by other developers. It is not realistic to write a specific function for any new type, which will cost a lot of time and money.

Generic can be the solution for this tricky situation. We don’t have to decide the data type initially, and we need to determine data type in runtime.

public class MyList<T>
     {
         private List<T> myList = new List<T>();
         public void addItem(T item)     
         {     
             myList.Add(item);     
         }     
         public T GetItemAt(int i)     
         {         
             return myList[i];     
         } 
}

As you can see, we replace the data type with a T in the above code. You don’t have to use T to represent a data type; you can use any other letters or words you are comfortable with.

Most importantly, we don’t know the data type at this stage; we determine the data type in run time. Here is an example.

class Program
     {
         static void Main(string[] args)
         {
             var stringList = new MyList<string>();
             stringList.addItem("Apple");
     } }

As you can see, we decide the List is holding a group of string items in runtime.


3 Replies to “Generic in C# – Basic Practice Example”

  1. I wanted to post you that little bit of observation to say thanks once again for your personal magnificent pointers you’ve featured in this article. It is quite remarkably generous of people like you to give openly just what a number of us might have marketed as an e book to generate some dough for themselves, most notably given that you could possibly have done it if you ever decided. These points as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth lots more pertaining to this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked at your blog post.

  2. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trustworthy, revealing as well as unique tips on your topic to Mary.

  3. Thanks for the recommendations on credit repair on this amazing site. A few things i would tell people is always to give up the actual mentality they can buy today and pay out later. Like a society we all tend to do that for many issues. This includes getaways, furniture, and also items we would like. However, you should separate the wants out of the needs. While you’re working to raise your credit score make some sacrifices. For example it is possible to shop online to save cash or you can visit second hand retailers instead of pricey department stores regarding clothing.

Leave a Reply

Your email address will not be published. Required fields are marked *