Book Image

.NET 4.0 Generics Beginner's Guide

By : Sudipta Mukherjee
Book Image

.NET 4.0 Generics Beginner's Guide

By: Sudipta Mukherjee

Overview of this book

Generics were added as part of .NET Framework 2.0 in November 2005. Although similar to generics in Java, .NET generics do not apply type erasure but every object has unique representation at run-time. There is no performance hit from runtime casts and boxing conversions, which are normally expensive..NET offers type-safe versions of every classical data structure and some hybrid ones. This book will show you everything you need to start writing type-safe applications using generic data structures available in Generics API. You will also see how you can use several collections for each task you perform. This book is full of practical examples, interesting applications, and comparisons between Generics and more traditional approaches. Finally, each container is bench marked on the basis of performance for a given task, so you know which one to use and when. This book first covers the fundamental concepts such as type safety, Generic Methods, and Generic Containers. As the book progresses, you will learn how to join several generic containers to achieve your goals and query them efficiently using Linq. There are short exercises in every chapter to boost your knowledge. The book also teaches you some best practices, and several patterns that are commonly available in generic code. Some important generic algorithm definitions are present in Power Collection (an API created by Wintellect Inc.) that are missing from .NET framework. This book shows you how to use such algorithms seamlessly with other generic containers. The book also discusses C5 collections. Java Programmers will find themselves at home with this API. This is the closest to JCF. Some very interesting problems are solved using generic containers from .NET framework, C5, and PowerCollection Algorithms ñ a clone of Google Set and Gender Genie for example! The author has also created a website (http://www.consulttoday.com/genguide) for the book where you can find many useful tools, code snippets, and, applications, which are not the part of code-download section
Table of Contents (20 chapters)
.NET 4.0 Generics
Credits
Foreword
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
2
Lists
4
LINQ to Objects
Migration Cheat Sheet

Time for action — consuming our new Extension method


Now, let's see how we can consume this method:

  1. 1. Stay in the project you created. Add a console application. Call it StringExTest.

  2. 2. Add a reference of StringEx class library to this project:

  3. 3. Add the following using directive in Program.cs:

    using StringEx;
    
  4. 4. Add the following string variable in the Main() method. This validates Swedish zip codes:

    string pattern = @"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$";
    
  5. 5. Add a few test input strings in a List of strings:

    List<string> testInputs = new List<string>() {"12345", "425611", "932 68", "S-621 46", "5367", "31 545" };
    

    Iterate through all these test inputs to find out whether they match the given regular expression pattern or not:

    foreach (string testInput in testInputs)
    {
    if (testInput.IsMatching(pattern))
    Console.WriteLine(testInput + " is a valid Swedish ZIP code");
    else
    Console.WriteLine(testInput + " is NOT a valid Swedish ZIP code");
    }
    

When run, this program should generate...