Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former.

C# Generics vs C++ Templates

I regularly meet with a group of my college friends with whom I studied Computer Science. While at a great local bar we have plenty of interesting computer-related conversations. A lot of .NET developers probably spend most of their time discussing technical topics with other .NET developers. Our conversations are much more interesting because everyone there works with different languages and different types of technology. It really adds a lot to our discussions. So as has happened a few times, I mentioned C# Generics. As usual it sparked a conversation about how C# Generics are not as good as C++ Templates. As usual I agree with them. (Yes, I really do like C++ templates better than I like Generics in C#)

So I searched on MSDN, because I wanted to see what Microsoft cites as the key differences between C++ templates and C# Generics. I found this article explaining the differences between C++ templates and C# Generics. OK, I agree these are some important differences. It says on there specifically that they were not even trying to have as much functionality as templates. It is good that they said that, because the list of "differences" could be renamed "reasons why templates are better".

The list of reasons cites a bunch of differences in how they are able to be used. I still want to know the differences in the implementation of them. For that big important difference they have it separated from the nice list. Its in the paragraph of description. They mention one HUGE difference. As they say, "C# generic type substitutions are performed at runtime and generic type information is thereby preserved for instantiated objects." So what is the big deal you might say. There are good things and bad things here for generics.

Since templates are compiled at runtime, the compiler will figure out every version of the template it needs and will create the code for them. This is great because it gives optimal performance. Sure there is a bit more code, but it is nice to not have to create these methods at runtime. With C# Generics, the required code is created at runtime. Ok so that sounds pretty bad. It isn't really as bad as it sounds. Generics are implemented smartly, and because of this it isn't much of a performance issue. So at runtime our code will be created to handle the different types our generic can handle. What if we're going to have a bunch of types? If they're all value types then you could have a problem. It will actually create a completely new implementation of your code for every value type you use for the generic. If you're using a reference type it uses some cool tricks to point to the correct type, so it only has to have one reference type implementation. This means you'll have the performance hit once when you first need the generic on a reference type. Then it is a minimal hit each other time you need it.

Yes, I was a C++ programmer before I learned C#. I hope you enjoyed this post. I appreciate comments. I really like the ones which add something to the post, so if you've got extra information to add I encourage you do so. If I've made any mistakes in this, please let me know.

Sponsor

Published Wednesday, January 30, 2008 9:44 AM by Brendan

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: C# Generics vs C++ Templates @ Thursday, February 14, 2008 1:38 PM

Well, I would say most of those differences are reasons why generics are better than templates. Really, the major defect of templates is their complexity. You can do so much, but creating so much confusion on anyone not adept on some of their patterns. In my life I've met very few big experts on templates (while having worked in many C++ only environments) and, anyway, knowledge is spread in different way. Wring code other people can't understand is detrimental to the project and, sadly, most uses of templates except the trivial ones are so.

San

# re: C# Generics vs C++ Templates @ Friday, February 22, 2008 6:11 PM

The same kinds of differences can be found throughout all aspects of comparing these two languages. Yes, templates are more complex, but so is C++ in general. It's like the difference between Windows and *nix (but on a different scale). Both can roughly serve the same functions, but some tasks lend themselves better to one or the other. I am a fan of C#, even though I started in C++ - but I'm never too happy about something being hidden from/unavailable to me just because someone who doesn't know what they're doing might write some bad code.

Brian

# re: C# Generics vs C++ Templates @ Sunday, February 24, 2008 12:53 AM

I could not agree with you more Brian. For pretty much all programs I would rather be writing in C#. It is a great language, and makes tasks much simpler. It bugs me as well when functionality is hidden away from me because of the dangerous potential of the functionality.

Thanks for your added insight.

Brendan

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below