Custom Software Blog Custom Software Consulting

C# Generics Summary: Methods, Types and Specifying Generic Type Parameter Constraints

by Aristo Setiawan 12/2/2009 3:55:00 AM

This article was written based on a presentation given by Timothy Bussmann.

Generics in C# are classes, structures, interfaces, and methods that have type parameters for one or more of the types that they use. For example, instead of using a collection class that allows any type, we can use a generic collection class and define the specific type(s) for the collection. The primary use case of generics is indeed to create type-safe generic collections and enumerators. Generics can also be used in lambda expressions, delegates and utility code.

There are a number of benefits of using generics. First, it promotes code reusability by writing code that is independent of the type(s) used for generic type parameter(s). Performance over object collections is improved because no runtime casts or boxing conversions need to be performed. Lastly, generics promote type safe programming and allow type inference to determine type parameters without requiring explicit specification.

Lambda Expressions and Func<>

A Lambda Expression is an anonymous function that contains an expression or statements is essentially a shorter way of writing a delegate. The Func<T, TResult> delegates use type parameters to define how many and, if necessary, the type(s) of input parameters and the return type of the delegate. Following are a few examples below on how lambda expressions are used in .NET:

  1. The following code:
    Clicked += delegate(object sender, EventArgs args) { DoSomething(); }
    Can be replaced with:
    Clicked += (sender, args) => { DoSomething(); }
  2. It is used frequently in LINQ:
    ordersTable.Where(order => order.IsPending);
    ordersTable.Select(order =>
        new { order.OrderID, order.Name, order.IsPending } );

Note that even though arguments are not specified, they remain type-safe. The types are simply inferred based on program information available to the compiler.

Constraints

When defining a generic class, you can apply restrictions to what types can be used as the arguments. These restrictions are called Constraints and applied by using where keyword. It is used to guarantee that a call to this method conforms to these constraints, and thus access the functionality defined by the constraints. These are the different types of constraints:

More...

Currently rated 4.2 by 5 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

Powered by BlogEngine.NET 1.4.5.0