Microsoft & .NET.NETTerm of the Week: Generics

Term of the Week: Generics content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

What are generics? Also known as parameterized types, generics have been around a long time in some languages, such as C++ with the Standard Template Library collection of parameterized types.

Developers using either Microsoft’s .NET languages and tools or Sun’s Java have had to do without generics. Several third party efforts have released generics retrofits for Java over the years, but generics has never been a feature of the Java specification.

The lack of generics is changing for both development platforms with the next releases of Microsoft’s .NET Framework and corresponding Visual Studio .NET toolset codenamed Whidbey and Sun’s Java SDK 1.5 codenamed Tiger. Both Microsoft and Sun are touting generics as a major improvement in their languages. If you develop with either of these platforms, generics is a term you’ll be seeing a lot more of in the future. It is interesting to note that Microsoft initially announced generics as a C# “Whidbey” feature but later announced generics would be supported in “Whidbey” in Visual Basic, Visual C++, and Visual J# as well.

It appears that someone at both Microsoft and Sun thinks generics are important.

You may still need to know what generics are and why they are important. Java and Visual Basic programmers may be experienced at writing the same code multiple times so that it works with different data types. The addition of generics (or parameterized types) to these platforms solves this problem by allowing a single class to work with many different types, within certain constraints. For instance, using generics in Visual Basic Whidbey could allow a class that could use the same code to work with either integers, decimals, or strings.

Generics quickly become important to developers for several reasons. Once you understand them, they’ll speed up future development work by allowing you to reuse the same algorithms for different types. A secondary related benefit of code reuse is reliability. Once you’ve successfully tested code the code you want to reuse, you won’t be rewriting and retesting it, potentially introducing new errors, just to use it with a different type. And a third benefit is that generics will reduce the number of times you need to perform explicit conversions between data types.

Both the Sun and Java implementations verify the generic types at compile time to avoid errors at run time. However, Sun and Microsoft take different approaches to insuring that generics are still type-safe. The Sun implementation essentially compiles and expands the generics code at compile time, placing the burden of dealing with generics at the compiler level and on the vendors who write the compilers. Microsoft states that their generic implementation has an advantage over the Java implementation in that they have rewritten the CLR and the languages to allow generics as a “first class” part of Whidbey and while they check for type safety at compile time, the generics types are left in as a run time feature through a rewriting of the CLR and languages.

Both Microsoft and Sun avoid too many direct comparisons to C++ templates. They also claim one main advantage over the C++ template generics implementation. Both claim that their generic implementations avoid the code bloat they see associated with C++ templates. Microsoft further claims that by not compiling out the generics as Java does, they avoid what they see as a code bloat problem in the Java implementation similar to the C++ code bloat.

For more details specific to Microsoft and Sun’s generics implementations, see:

Java Generics

.NET Generics

You can also check out other articles:

What Are C# Generics?

Are Java Developers Ready for Hunting Tiger?

J2SE 1.5 – Effective Java Programming with Tiger

# # #

Jim Minatel is a freelance writer for in addition to working with Wiley and WROX publishing.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories