Skip to Content
Professional C# 5.0 and .NET 4.5.1
book

Professional C# 5.0 and .NET 4.5.1

by Christian Nagel, Jay Glynn, Morgan Skinner
February 2014
Intermediate to advanced
1560 pages
50h 21m
English
Wrox
Content preview from Professional C# 5.0 and .NET 4.5.1

Chapter 5Generics

WHAT’S IN THIS CHAPTER?

  • An overview of generics
  • Creating generic classes
  • Features of generic classes
  • Generic interfaces
  • Generic structs
  • Generic methods

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at www.wrox.com/go/procsharp on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Linked List Objects
  • Linked List Sample
  • Document Manager
  • Variance
  • Generic Methods
  • Specialization

GENERICS OVERVIEW

Since the release of .NET 2.0, .NET has supported generics. Generics are not just a part of the C# programming language; they are deeply integrated with the IL (Intermediate Language) code in the assemblies. With generics, you can create classes and methods that are independent of contained types. Instead of writing a number of methods or classes with the same functionality for different types, you can create just one method or class.

Another option to reduce the amount of code is using the Object class. However, passing using types derived from the Object class is not type safe. Generic classes make use of generic types that are replaced with specific types as needed. This allows for type safety: the compiler complains if a specific type is not supported with the generic class.

Generics are not limited to classes; in this chapter, you also see generics with interfaces and methods. Generics with delegates can be found in Chapter 8, “Delegates, Lambdas, and Events.”

Generics ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro C# 2010 and the .NET 4 Platform, Fifth Edition

Pro C# 2010 and the .NET 4 Platform, Fifth Edition

Andrew Troelsen
ASP.NET 2.0 Illustrated

ASP.NET 2.0 Illustrated

Alex Homer, Dave Sussman
Professional Visual Studio® 2008

Professional Visual Studio® 2008

Nick Randolph, David Gardner

Publisher Resources

ISBN: 9781118832981Purchase book