Back to description
For many programmers, generics will be an entirely new language feature. As such, it is important to establish a foundation... more
For many programmers, generics will be an entirely new language feature. As such, it is important to establish a foundation of concepts that will clarify the role and significance of generics in the overall scheme of the .NET platform. This chapter provides this fundamental, conceptual view of generics that should provide you with a solid base of ideas that can be built upon in the chapters that follow. Along the way, you’ll get the opportunity build your first generic types and get some exposure to the basic mechanics of generic types. This chapter also introduces a set of new terms that are used when referring to common generic concepts. You’ll need to have a clear understanding of these terms because they are used throughout the book. Naturally, if you’re already comfortable with the basics of generics, you may want to skip over this chapter.
... less
This book is filled with references to the importance of type safety. The term gets thrown around very loosely inside and... more
This book is filled with references to the importance of type safety. The term gets thrown around very loosely inside and outside the world of generics. So much so, that it seems like its meaning is often lost in the shuffle as a core value for many developers. Now, with generics, it’s worth reexamining the value of type safety because it’s one of the motivating factors that influenced the introduction of this new language feature. This chapter revisits the origins of type safety and discusses some of the unsafe trends that have become a common occurrence. Certainly, this is an area where there may be some disagreement. However, it’s an area that needs to be discussed as part of sharpening your awareness and understanding the impact generics will have on your everyday approach to designing and building solutions.
In the early stages of introducing generics, there was a fair amount of confusion surrounding the scope and capabilities... more
In the early stages of introducing generics, there was a fair amount of confusion surrounding the scope and capabilities of this new language feature. Many developers jumped to the conclusion that generics would be the functional equivalent of C++ templates. And, while these two constructs share common heritage, they definitely cannot be viewedin any respectas being one in the same. It’s important, especially for those with a template orientation, to understand how generics and templates differ. This information may also be helpful, in a more conceptual sense, to anyone transitioning to generics. Overall, after reading this chapter, you should come away with a much clearer picture of the fundamental differences that exist between generics and templates implementations.
Many developers will view themselves primarily as consumers of generics. However, as you get more comfortable with generics... more
Many developers will view themselves primarily as consumers of generics. However, as you get more comfortable with generics, you’re likely to find yourself introducing your own generic classes and frameworks. Before you can make that leap, though, you’ll need to get comfortable with all the syntactic mutations that come along with creating your own generic classes. The goal of this chapter, then, is to dig into all of the details associated with building generic classes, explaining how generics extend the existing rules for defining and consuming classes. Fortunately, as you move through this chapter, you’ll notice that the syntax rules for defining generic classes follow many of the same patterns you’ve already grown accustomed to with non-generic types. So, although there are certainly plenty of new generic concepts you’ll need to absorb, you’re likely to find it quite easy to make the transition to writing your own generic types.
As developers get acclimated to generics, they tend to focus their attention squarely on generic classes. And, although generic... more
As developers get acclimated to generics, they tend to focus their attention squarely on generic classes. And, although generic classes may represent a big part of what generics bring to the table, they only represent one facet of what can be achieved with generics. As an example, the .NET generics implementation also allows you to create individual generic methods. These methods employ the same concepts that are associated with generic classes. And, as you will see in this chapter, this ability to leverage generics at this finer level of granularity can come in quite handy. As part of looking at generic methods, this chapter covers all the basic mechanics associated with declaring and consuming a generic method. Along the way, you also see a few examples that illustrate different patterns for introducing generic methods into your existing solutions. Ultimately, you’re likely to find a number of opportunities to apply generic methods to your existing applications.
Delegates represent one of those subtle, helper mechanisms that can easily get overshadowed by some of the bigger concepts... more
Delegates represent one of those subtle, helper mechanisms that can easily get overshadowed by some of the bigger concepts found in the .NET Framework. However, they play an important role in the grand scheme of the framework and, given their nature, they were a natural fit to be extended and enhanced via generics. This chapter looks at all the facets of generic delegates as well as the advantages generic delegates have to offer over their non-generic counterparts. The chapter also explores how generics can be leveraged as part of other classes that are included in the framework. Overall, you should come away from this with a better grasp of the fundamental tools you need to consume and build your own generic delegates.
Generic constraints represent a key component of the .NET generics implementation, allowing you to constrain your type parameters... more
Generic constraints represent a key component of the .NET generics implementation, allowing you to constrain your type parameters to specific interfaces. Knowing the strengths and limitations that are associated with using constraints is vital to broadening your understanding of what can ultimately be achieved with generic types. This chapter looks at all the different mechanisms that are available to you when deciding how, when, and what types of constraints you want to apply to your type parameters. It also looks at each of the constraint types and discusses some of the ramifications associated with combining constraints. The chapter also considers some of the broader implications that accompany the application of constraints.
The potential and power of generics is most fully realized in the implementation of container frameworks. Given this reality... more
The potential and power of generics is most fully realized in the implementation of container frameworks. Given this reality, it only made sense that the introduction of generics would also be accompanied by the introduction of a pool of new generic types. In version 2.0 of the .NET Framework, the Base Class Library (BCL) introduces two new namespaces, System.Collections.Generics and System.Collections.ObjectModel, both of which include generic representations of many of the non-generic containers that already existed in the System.Collections namespace long before generics came along. The goal of this chapter is to provide you with an overview of the classes that appear in these namespaces, along with a roadmap for how and when you might want to apply each of these generic types. It also covers all the key methods and properties that are associated with each of these new types.
System.Collections.Generics
System.Collections.ObjectModel
System.Collections
The introduction of generics, in some respects, represents an extension of the existing .NET type system. And, of course,... more
The introduction of generics, in some respects, represents an extension of the existing .NET type system. And, of course, whenever types inherit new behavior, the APIs that interact with those types must also evolve to support those concepts. This chapter looks at three specific areas of the .NET Frameworkreflection, serialization, and remotingthat are directly influenced by the introduction of generic types. The bulk of this chapter focuses on reflection, describing how generic types are created, examined, and manipulated dynamically via the reflection API. The chapter also looks at how generic types are serialized and the role they can play in solutions that use remoting. Along the way, you’ll also see a few scenarios where generics can be applied to create more type-safe interactions with these APIs.
With each significant new language feature also comes a set of guidelines that dictate how and when that feature should be... more
With each significant new language feature also comes a set of guidelines that dictate how and when that feature should be applied. Generics are no different. This chapter assembles a set of guidelines that attempt to address some of the common practices that should be applied or given consideration when consuming or constructing generic types. As part of this effort, it provides an item-by-item breakdown of the guidelines and, where necessary, digs into the pros and cons associated with a given guideline. The goal here is to bring together, in one place, all those generics practices that are being discussed, debated, and adopted by the development community.
If you’re going to be working with generics, you’re also likely to have some interest in understanding how they’re managed... more
If you’re going to be working with generics, you’re also likely to have some interest in understanding how they’re managed by the CLR. This chapter looks at all the inner workings of generic types. It examines how generic types are instantiated at run-time, how they’re represented in IL, and how the CLR attempts to optimize their size and performance. It also explores some of the key motivating factors that influenced the overall design characteristics employed as part of the .NET generics implementation. The chapter wraps up by looking at some of the performance gains that can be achieved through the use of generics. By the time you reach the end, you should have a much better feeling for the underlying concepts that directly influence how you go about creating and consuming generics.
At this stage, it should be clear that generics and templates are different beasts. And, even though the syntax of C++ templates... more
At this stage, it should be clear that generics and templates are different beasts. And, even though the syntax of C++ templates shares similarities with generics, significant syntactic differences exist between the two. The goal of this chapter, then, is to provide an overview of the syntax that’s associated with creating and consuming generic types in C++. This chapter briefly touches on all the fundamental generic elements, including C++ generic examples of classes, methods, interfaces, and delegates. The chapter also explains how generics and templates can be combined to strike a balance between the power of templates and the interoperability of generics.
Throughout this book, the majority of the examples have used either C# or Visual Basic. These are certainly two of the more... more
Throughout this book, the majority of the examples have used either C# or Visual Basic. These are certainly two of the more heavily used languages of the .NET platform. At the same time, the J# language also includes support for generics, and its generics nuances are certainly worth exploring. This chapter looks at how J# can be used with many of the generic constructs (classes, methods, and so on). It revisits some of the key generic concepts and explains how they take shape within the J# language. The main idea here is to explore the fundamentals of J# generics and provide some concrete examples of J# generics in action. Because many J# programmers are Java transplants, this chapter also points out some of the differences between the Java and .NET generics implementations. Overall, after looking at some of the sample code in this chapter, you’ll likely discover that using generics with J# isn’t all that different from the other languages of the .NET platform.
The .NET platform already provides developers with a rich set of generic types. And, though this collection represents a... more
The .NET platform already provides developers with a rich set of generic types. And, though this collection represents a good start, there’s always going to be room for improvement. In fact, a handful of teams have already been hard at work compiling their own generic libraries. Of all of these offerings, the Power Collections frameworkthe focus of this chapterappears to have the most momentum and support from Microsoft. This library appears to draw its inspiration from a combination the C++ Standard Template Library and community input, extending the Base Class Library (BCL) with an Algorithms class and a series of new containers. This chapter digs into the details of this open source, continually evolving library. The chapter starts with a high-level, conceptual view of all the types in the library and follows that up with a detailed look at the mechanics of using each type.
Algorithms
Purchase Before purchasing this product, please be sure you have met all software and system requirements, and that you understand any limits placed upon its use.
Return Policy Wrox Chapters on Demand are non-returnable and non-refundable.
Reader Software Wrox Chapters on Demand are offered as PDFs, and they must be viewed using the Adobe Reader. If you do not have the Reader installed, it can be downloaded for free at Adobe.com.
Test Download As Wrox Chapters on Demand purchases are non-returnable, it is advisable that you test your system and software configurations with a free sample download before you place an order.
Usage Rights for a Wrox Chapter on Demand File Any Wrox Chapter on Demand product you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:
If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).
Related Books
.NET Resources