Home >

Generics ? What you should know?

11. September 2009

I have had some interesting conversations over the last week or so about generics. It was brought up out of a MCTS study group and then in the internal DNUG that I lead. so let me break it down.

Without Generics -

I have to setup collections of objects manual and for each type that I want to use. To add, sort, filter, or aggregate this collection takes a lot of code and time. Some things like ArrayList return objects which means all that casting or reflection over head to get into the objects I need.

With Generics -

I have to setup a collection and give it the input types. It handles sorting, filtering, adding, and aggregates through the use of generic Action<T>, Func<T>, or Predicate<T>. I can write sorts and filters and make them generic so that I don?t have to copy/paste change type.

Generics are not the end all and be all but they help a great deal when you are trying not to repeat yourself. I have found that when using generics that the compiler actually spits out the methods with the implemented types in them, which means that generics while great are syntactic sugar, and I have a sweet tooth.

With Linq being based on the use of IEnumerable<T> and the new parallel extensions to .NET 4.0 being able to spread the workload of Action<T> and Task<T> across multiple processors I would tell everyone. Start learning generics. It is easy to use once you wrap your head around it. Like if I wanted to add two numbers of different types I could write this:

public float Add(int one, decimal two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

public float Add(decimal one, decimal two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

public float Add(int one, int two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

public float Add(double one, double two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

public float Add(int one, double two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

public float Add(double one, decimal two)
{
       float item = new float();
       item = float.Parse(one.ToString()) + float.Parse(two.ToString());
       return item;
}

or I could write this:

public float Add<T, T2>(T one, T2 two)
{
      return float.Parse(one.ToString()) + float.Parse(two.ToString());
}

Which would you prefer?

 

Enjoy, and remember.

Code Like you have to support it.

Comments

3/17/2010 4:37:48 AM #
This is my first time here, really good looking blog. I discovered a lot of fascinating things in your blog particularly its discussion. From all the remarks on your posts, it appears like this is really a extremely popular website. Keep up the good work.
3/24/2010 3:48:06 AM #
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
3/24/2010 11:22:59 AM #
hopefully this post doesn't appear several times (it seems to freeze once i try to post my comment.. not sure if it's actually posting), but all I really wanted to say was great post and thanks for sharing.
3/25/2010 6:04:06 AM #
As a Noob, I am always seeking online for articles that can help me. Thank you
3/25/2010 12:21:33 PM #
can anyone tell me how to get the little avatars to appear in my comments section? thanks!
3/26/2010 9:38:18 AM #
Terrific post, good work. It Couldn't be composed any greater. Reading this post reminds me of my former dentist! He constantly kept talking about this. I will forward this article to him. Pretty sure he will have a great read. Thanks for sharing!
3/26/2010 3:43:47 PM #
Hi there, I found your blog via Google while searching for first aid for a heart attack and your post looks very interesting for me.
3/27/2010 5:09:26 AM #
can anyone tell me how to get those little avatars to appear in my comments section? thank you!
3/27/2010 9:20:05 PM #
I wanted to thank you for this great I definitely loved every little bit of it. I have you bookmarked your site to look at the latest stuff you post.
3/28/2010 5:20:58 AM #
Hi. I just noticed that your blog looks like it has a few code problems at the very top of your website's page. I'm not sure if everybody is getting this same problem when browsing your website? I am employing a totally different browser than most people, referred to as Opera, so that is what might be causing it? I just wanted to make sure you know. Thanks for posting some great postings and I'll try to return back with a completely different browser to check things out!
3/28/2010 1:12:44 PM #
A thoughtful insight and ideas I will use on my blog. You've obviously spent a lot of time on this. Congratulations!
3/28/2010 8:17:37 PM #
This is a well designed web page
I love reading your posts.
You have valuable information here.









3/28/2010 8:39:31 PM #
I can see that you are an expert in this area. I am starting a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
3/29/2010 1:00:15 AM #
I think am just having some problems with subscribing to RSS feed here.
3/29/2010 5:02:36 AM #
Good article, thank you. I just signed up to your blog RSS.
3/29/2010 2:11:30 PM #
There is obviously a lot to know about this.  I think you made some good points in Features also.
3/29/2010 6:12:29 PM #
Such a Wonderful job for your blog. It's incredibly informative and to the point. I am giving it 5 stars and enjoy reading a lot more within the future and hope you have a wonderful day.
3/30/2010 2:39:54 AM #
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
3/30/2010 5:51:03 PM #
I can tell that you are putting lots of determination into your blog and detailed articles! I'm deeply gets interested each and every single piece of info you post here (you will discover not numerous quality blogs left .
3/31/2010 5:54:23 PM #
Such a Good job for your blog. It's incredibly informative and to the point. I am giving it 5 stars and look forward to reading more within the future and hope you have a great day.
3/31/2010 6:13:50 PM #
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
4/3/2010 9:58:59 AM #
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
4/7/2010 6:39:36 AM #
I bookmarked this blog a while ago because of the smart content and I am never being dissapointed. Continue the good work.
4/8/2010 12:57:45 AM #
I never realised this earlier, but you have got a precise admirable point indeed!
4/8/2010 4:41:25 AM #
I have lately started my own blog therefore its great to pickup hints from what you have going here. Numerous Thanks.
4/8/2010 9:27:45 PM #
Fantastic solution and well published post. I understood it right away!
4/9/2010 9:11:04 AM #
I was just going to relax and read this morning then in the meantime I got your blog to read. Really good one.
4/9/2010 6:42:20 PM #
Nice blog. Keep on writing
4/10/2010 11:59:32 PM #
Work is much more fun than fun.
4/11/2010 1:01:32 AM #
Thanks for another useful post. I am pretty sure this post has helped me save many hours of scrolling through other similar posts just to find what I was looking for. I just want to say: Thank you!
4/13/2010 10:00:32 AM #
can someone tell me how to get those little avatars to appear in my comments section? thanx!
4/17/2010 8:37:37 AM #
I bookmarked this blog a while ago because of the fantastic content and I am never being dissapointed. Continue the good work.
4/17/2010 9:01:21 PM #
This was a nice post and I think it is pretty easy to see from the other comments as well that this post is well written and informative. Keep up the good work.
4/18/2010 5:11:23 AM #
I can see that you're putting plenty of effort and time into your blog and detailed articles! I'm deeply in love with each single piece of facts you post here (you will find not quite a few quality blogs left .
4/19/2010 6:17:14 PM #
I bookmarked this blog a while ago because of the good content and I am never being dissapointed. Continue the good work.
4/19/2010 9:05:27 PM #
This was just what I was on the look for. I'll come back to this blog for sure!
4/19/2010 10:09:42 PM #
Thanks for another informative post. I am quite sure this article has helped me save many hours of scrolling through other similar posts just to find what I was looking for. Keep up the good work: Thank you!
4/20/2010 5:00:49 PM #
Smartblog but I had some problems watching it in IE. know why?
4/20/2010 7:35:37 PM #
This was just what I was looking for. I'll come back to this blog for sure!
4/20/2010 9:23:58 PM #
Another smart post. This is a very nice blog that I will definitively come back to several more times this year!
4/21/2010 8:49:36 PM #
Thanks for another great post. I am completely sure this post has helped me save many hours of browsing other similar posts just to find what I was looking for. I just want to say: Thank you!
4/22/2010 6:31:18 PM #
I bookmarked this blog a while ago because of the smart content and I am never being dissapointed. Continue the good work.
4/23/2010 6:22:17 PM #
I bookmarked this blog a while ago because of the awesome content and I am never being dissapointed. Keep up the good work.
4/25/2010 8:45:01 PM #
I bookmarked this blog a while ago because of the smart content and I am never being dissapointed. Continue the good work.
4/25/2010 9:34:13 PM #
I bookmarked this blog a while ago because of the helpful content and I am never being dissapointed. Keep up the good work.
4/25/2010 10:30:39 PM #
This was just what I was searching for. I'll bookmark this blog for sure!
4/26/2010 5:15:34 PM #
Thanks for another nice post. I am pretty sure this post has helped me save many hours of scrolling through other similar posts just to find what I was looking for. Keep up the good work: Thank you!
4/26/2010 5:32:24 PM #
can someone tell me how to get those little avatars to show up in my comments section? thank you!
4/26/2010 7:47:51 PM #
I bookmarked this blog a while ago because of the nice content and I am never being dissapointed. Keep up the good work.
4/27/2010 8:29:45 PM #
Thanks for another awesome post. I am completely sure this article has helped me save many hours of reading other similar posts just to find what I was looking for. I just want to say: Thank you!
4/27/2010 9:35:03 PM #
I bookmarked this blog a while ago because of the nice content and I am never being dissapointed. Continue the good work.
4/28/2010 8:06:40 PM #
Niceblog but I had some problems watching it in FFox. know why?
4/28/2010 9:45:12 PM #
I bookmarked this blog a while ago because of the great content and I am never being dissapointed. Continue the good work.
4/29/2010 6:39:42 PM #
Melia13@gmail.com
4/29/2010 9:36:39 PM #
Smartblog but I had some problems watching it in Chrome. know why?