Export (0) Print
Expand All
Abortable Thread Pool
The Analytic Hierarchy Process
API Test Automation in .NET
Asynchronous HttpWebRequests, Interface Implementation, and More
Bad Code? FxCop to the Rescue
Basics of .NET Internationalization
Behind the Scenes: Discover the Design Patterns You're Already Using in the .NET Framework
BigInteger, GetFiles, and More
Binary Serialization of DataSets
Building Voice User Interfaces
Can't Commit?: Volatile Resource Managers in .NET Bring Transactions to the Common Type
CLR Inside Out: Base Class Library Performance Tips and Tricks
CLR Inside Out: Ensuring .NET Framework 2.0 Compatibility
CLR Inside Out: Extending System.Diagnostics
CLR Profiler: No Code Can Hide from the Profiling API in the .NET Framework 2.0
Concurrent Affairs: Build a Richer Thread Synchronization Lock
Custom Cultures: Extend Your Code's Global Reach With New Features In The .NET Framework 2.0
Cutting Edge: Collections and Data Binding
Const in C#, Exception Filters, IWin32Window, and More
Creating a Custom Metrics Tool
DataGridView
DataSets vs. Collections
Determining .NET Assembly and Method References
Experimenting with F#
File Copy Progress, Custom Thread Pools
Finalizers, Assembly Names, MethodInfo, and More
Got Directory Services?: New Ways to Manage Active Directory using the .NET Framework 2.0
High Availability: Keep Your Code Running with the Reliability Features of the .NET Framework
How Microsoft Uses Reflection
ICustomTypeDescriptor, Part 2
ICustomTypeDescriptor, Part 1
Iterating NTFS Streams
JIT and Run: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects
Lightweight UI Test Automation with .NET
Low-Level UI Test Automation
Make Your Apps Fly with the New Enterprise Performance Tool
Managed Spy: Deliver The Power Of Spy++ To Windows Forms With Our New Tool
Memory Models: Understand the Impact of Low-Lock Techniques in Multithreaded Apps
Microsoft Java Virtual Machine Update
Microsoft .NET Framework Delivers the Platform for an Integrated, Service-Oriented Web, Part 2
Mini Dump Snapshots and the New SOS
Mutant Power: Create A Simple Mutation Testing System With The .NET Framework
NamedGZipStream, Covariance and Contravariance
.NET Internationalization Utilities
.NET Profiling: Write Profilers With Ease Using High-Level Wrapper Classes
No More Hangs: Advanced Techniques To Avoid And Detect Deadlocks In .NET Apps
The Perfect Host: Create and Host Custom Designers with the .NET Framework 2.0
Phoenix Rising
Scheme Is Love
Security Enhancements in the .NET Framework 2.0
Sepia Tone, StringLogicalComparer, and More
Software Testing Paradoxes
Stay Alert: Use Managed Code To Generate A Secure Audit Trail
Stream Decorator, Single-Instance Apps
StringStream, Methods with Timeouts
SUPERASSERT Goes .NET
Tailor Your Application by Building a Custom Forms Designer with .NET
Test Harness Design Patterns
ThreadPoolPriority, and MethodImplAttribute
ThreadPoolWait and HandleLeakTracker
Three Vital FXCop Rules
A Tidal Wave of Change
To Confirm is Useless, to Undo Divine
Touch All the Bases: Give Your .NET App Brains and Brawn with the Intelligence of Neural Networks
Transactions for Memory
Trustworthy Software
Tune in to Channel 9
UDP Delivers: Take Total Control Of Your Networking With .NET and UDP
UI on the Fly: Use the .NET Framework to Generate and Execute Custom Controls at Run Time
Unexpected Errors in Managed Applications
Unhandled Exceptions and Tracing in the .NET Framework 2.0
Using Combinations to Improve Your Software Test Case Generation
Wandering Code: Write Mobile Agents In .NET To Roam And Interact On Your Network
What Makes Good Code Good?
XML Comments, Late-bound COM, and More
Expand Minimize
22 out of 48 rated this helpful - Rate this topic

Generics FAQ: .NET Framework

 

Juval Lowy

October 2005

Applies to:
   Microsoft .NET Framework

Summary: Review frequent asked questions about the Microsoft .NET Framework. (35 printed pages)

Contents

Which Versions of the .NET Framework Support Generics?
Can I Use Generics in Web Services?
Can I Use Generics in Enterprise Services?
Can I Use Generics in Indigo?
Can I Use Generics in .NET Remoting?
Can I Use Visual Studio 2003 or the .NET Framework 1.1 to Create Generics?
What Environment Do I Need to Use Generics?
Can I Use Generics on the Compact Framework?
Which .NET Languages Support Generics and How?
Where Does the .NET Framework Itself Use Generics?
What Are the Generic Collection Classes?
What Are the Generic Delegates?
What Are the Generic Methods of System.Array?
What Are the Generic Methods of List<T>?
What Are Nullable Types?
How Do I Reflect Generic Types?

Which Versions of the .NET Framework Support Generics?

Generics are only supported on version 2.0 and above of the Microsoft .NET framework, as well as version 2.0 of the compact framework.

Can I Use Generics in Web Services?

Unfortunately, no. Web services have to expose a WSDL-based contract. Such contracts are always limited by the expressiveness of the message format being used. For example, HTTP-GET based web services only support primitive types such as int or string, but not complex types like a DataSet. SOAP-based web services are more capable, but SOAP has no ability to represent generic type parameters. As a result, at present, you cannot define web services that rely on generic types. That said, you can define .NET web services that rely on closed constructed generic types, for example:

[C#]

public class MyWebService 
{
   [WebMethod]
   public List<string> GetCities() 
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisco");
      cities.Add("London");
      return cities;
   }
}

[Visual Basic]

Public Class MyWebService
    <WebMethod>
    Public Function GetCities() As List(Of String)
        Dim cities As New List(Of String)()
        cities.add("New York")
        cities.add("San Francisco")
        cities.add("London")
        Return cities
    End Function
End Class

[Visual C++]

public ref class MyWebService 
{
public:
   [WebMethod]
   List<String ^> ^ GetCities() 
   {
      List<String ^> ^cities = gcnew List<String ^>;
      cities->Add("New York");
      cities->Add("San Francisco");
      cities->Add("London");
      return cities;
   }
}

In the above example, List<string> will be marshaled as an array of strings.

Can I Use Generics in Enterprise Services?

Unfortunately, no. All methods and interfaces on a ServicedComponent-derived class must be COM-visible. The COM type system is IDL, and IDL does not support type parameters.

Can I Use Generics in Indigo?

Unfortunately, no. SOAP has no ability to represent generic type parameters, and so all methods and interfaces on an indigo service contract or service class can only use primitive types such as integers or strings, or specific known types that provide a data contract. As a result, at present, you cannot define Indigo services that rely on generic types, that is, services that leave it up to the service consumer to specify the types to use when invoking the service.

Can I Use Generics in .NET Remoting?

Yes. You can expose generic types as remote objects, for example:

[C#]

public class MyRemoteClass<T> : MarshalByRefObject
{...}
Type serverType = typeof(MyRemoteClass<int>);

RemotingConfiguration.RegisterWellKnownServiceType(serverType,
                                                   "Some URI",
                                                  WellKnownObjectMode.SingleCall);

[Visual Basic]

Public Class MyRemoteClass(Of T)
    Inherits MarshalByRefObject
    ...
End Class

Dim serverType As Type = GetType(MyRemoteClass(Of Integer))
RemotingConfiguration.RegisterWellKnownServiceType(serverType, _
                                                   "Some URI", _
                                                   WellKnownObjectMode.SingleCall)

[Visual C++]

generic <typename T>
public ref class MyRemoteClass : MarshalByRefObject
{...};
Type ^serverType = typeid<MyRemoteClass<int> ^>;

RemotingConfiguration::RegisterWellKnownServiceType(serverType,
                                                   "Some URI",
                                                  WellKnownObjectMode::SingleCall);

Note that the specific type arguments used must be a marshalable type, that is, either serializable or derived from MarshalByRefObject. Consequently, a generic remote type will typically place a derivation constraint from MarshalByRefObject on its generic type parameters when expecting reference type parameters:

[C#]

public class MyRemoteClass<T> : MarshalByRefObject where T : MarshalByRefObject
{...}

[Visual Basic]

Public Class MyRemoteClass(Of T As MarshalByRefObject)
    Inherits MarshalByRefObject
    ...
End Class

[Visual C++]

generic <typename T> where T : MarshalByRefObject
public ref class MyRemoteClass: MarshalByRefObject
{...};

To administratively register a generic type, provide the type arguments in double square brackets.

For example, to register the class MyRemoteClass<T> with an integer, you should write:

<service> 
   <wellknown type="MyRemoteClass[[System.Int32]],ServerAssembly" 
              mode="SingleCall" objectUri="Some URI"/> 
</service>  

The double square brackets is required in case you need to specify multiple type arguments, in which case, each type arguments would be encased in a separate pair of brackets, separated by a comma. For example, to register the class MyRemoteClass<T,U> with an integer and a string, you would write:

<service> 
   <wellknown type="MyRemoteClass[[System.Int32],[System.String]],
                     ServerAssembly" mode="SingleCall" objectUri="Some URI"/> 
</service>  

Creating a new instance of generic remote objects is done just as with non-generic remote objects.

Can I Use Visual Studio 2003 or the .NET Framework 1.1 to Create Generics?

Unfortunately no. Generics are only supported on version 2.0 and above of the Microsoft .NET framework. Code that relies on generics must run on version 2.0 of the CLR. Because of the way the CLR version unification works, a run-time process can only load a single version of the CLR. Consequently, a process that loaded version 1.1 of the CLR cannot use generic types. If you must use generic types from .NET 1.1, you can use the following work-around: First, wrap the generic types with object-based types (at the expense of course of the benefits of using generics). Next, load the wrapper classes in a separate process which loads version 2.0 of the CLR, and provide remote access to the wrapper classes to legacy clients in process that use version 1.1 of the CLR. For remote communication you can use any number of cross-process communication mechanisms, such as Remoting, Enterprise Services, sockets, etc.

What Environment Do I Need to Use Generics?

To deploy and run code that uses generics you need version 2.0 or higher of the .NET runtime.

Can I Use Generics on the Compact Framework?

Yes. The .NET Compact Framework version 2.0 supports generics. Like most other things with the .NET Compact Framework, the generics support is very close but not exactly the same as the normal .NET Framework, due to performance and schedule constrains. You can use generics with both C# and Visual Basic for the compact framework. The compact framework does apply certain limitations on generics, the notable ones are:

  • The compact framework does not verify constraints are runtime, only at compile time.
  • You can only have up to 8 generic type parameters per generic type.
  • You cannot use reflection on unbounded generic types.

Which .NET Languages Support Generics and How?

Both C# 2.0 and Visual Basic 2005 support defining and consuming generics. Visual C++ 2005 also supports generics in addition to classic C++ templates. Visual J# 2005 supports consuming generic types but not defining them. At present, it is not known of other vendors besides Microsoft that added generics support for their languages.

Where Does the .NET Framework Itself Use Generics?

Version 2.0 of the .NET Framework makes use of generics in three main areas: The System namespace added a large set of static generic methods to the Array type. These methods automate and streamline common manipulations of and interactions with arrays. The System namespace also defined a number of generic utility delegates, which are used by the Array type and the List<T> class, but can be used freely in other contexts as well. In addition, System provides support for nullable types. The System namespace defines the IComparable<T> interface and the EventHandler<E> delegate, both generic reincarnations of their non-generic predecessors. The System namespace also defines the IEquatable<T> interface, used to check for equality of two values. The System namespace defines the ArraySegment<T> used to allocate a strongly typed portion of an array.

The System.Collections.Generic namespace defines generic collection interfaces, collections and iterator classes, similar to the old, non generic ones available in the System.Collections namespace. The System.Collections.Generic namespace also defines a few generic helper classes and structures.

The System.ComponentModel namespace defines the class BindingList<T>. A binding list is used very similar to a mere generic list, except it can fire events notifying interested parties about changes to its state.

The System.Collections.ObjectModel namespace defines a few types such as Collection<T> that can be used as base types for custom collections.

Finally, all the types that supported IComparable in .NET 1.1 support IComparable<T> and IEquatable<T> in .NET 2.0. This enables you to use common types for keys, such as int, string, Version, Guid, DateTime, and so on.

What Are the Generic Collection Classes?

The System.Collections.Generic namespace contains the majority of the new generic collections. These collections are by and large the generic reincarnation of the collections available in the System.Collections namespace. For example, there is a generic Stack<T> and a generic Queue<T> classes. The collections in System.Collections.Generic are used in much the same way as their predecessors. In addition, some of the collections where renamed in the process. The Dictionary<K,T> data structure is equivalent to the non-generic HashTable, and the class List<T> is analogous to the non-generic ArrayList. System.Collections.Generic also defines new types that have no equivalent in System.Collections, such as LinkedList<T> and KeyValuePair<K,T>. In addition, The System.Collections.Generic namespace defines generic interfaces such as ICollection<T> and IList<T>. To support generic-based iterators, System.Collections.Generic defines the IEnumerable<T> and IEnumerator<T> interfaces, and these interfaces are supported by all the generic collections. It is important to note that the generic collections can be used by clients that do not rely on generics, because all the generic collections also support the non-generic collection and iteration interfaces (IList, ICollection, IEnumerable). For example, here is the definition of the List<T> class:

[C#]

public class List<T> : IList<T>,IList
{...}

[Visual Basic]

Public Class List(Of T)
    Implements IList(Of T),IList
    ...
End Class 

[Visual C++]

generic <typename T>
public ref class List<T> : IList<T>
{...};

The System.ComponentModel namespace defines the type BindingList<T>.

[C#]

public class BindingList<T> : Collection<T>,
     IBindingList,ICancelAddNew,IRaiseItemChangedEvents
{
   public event ListChangedEventHandler ListChanged;
   public event AddingNewEventHandler AddingNew;

   public BindingList();
   public BindingList(List<T> list);
   public T AddNew();
   //More members 
}

[Visual Basic]

Public class BindingList(Of T)
    Inherits Collection(Of T)
    Implements IBindingList, ICancelAddNew, IRaiseItemChangedEvents
    Public Event ListChangedEventHandler ListChanged
    Public Event AddingNewEventHandler AddingNew
    Public Sub BindingList()
    Public Sub BindingList(ByVal list As List(Of T))
    Public Function AddNew() As T
    ' More members
End Class

[Visual C++]

generic <typename T>
public ref class BindingList : Collection<T>,IBindingList,
                               ICancelAddNew,IRaiseItemChangedEvents
{
public:
   event ListChangedEventHandler ^ListChanged;
   event ListChangedEventHandler ^ AddingNew;

   public: BindingList();
           BindingList(List<T> ^list);
           T AddNew();
   //More members 
}

BindingList<T> is used similarly to a generic list, except it can fire events notifying interested parties about changes to its state, so you can bind it to user interface controls such as the ListBox. You can use BindingList<T> directly or you can wrap it around an existing List<T>.

The System.Collections.ObjectModel namespace defines the types Collection<T>, KeyedCollection<T>, ReadOnlyCollection<T>, and ReadOnlyCollection<T> provided as base types for custom providers. Interestingly enough, none of the .NET-provided generic collections actually use these base collections.

Finally, the System namespace defines the ArraySegment<T> helper structure, which can be used to obtain a generic-based segment of a provided array.

The following table lists the generic collections and their supporting types, including mapping the generic collections to those of System.Collections or other namespaces when applicable.

Type Namespace Non-Generic Equivalent Comment
ArraySegment<T> System - Used to obtain a generic-based segment of a provided array
BindingList<T> System.ComponentModel - Linked list that fires state changes events
Collection<T> System.Collections.ObjectModel Collection Non abstract base class for other collections
Comparer<T> System.Collections.Generic Comparer Implements IComparer<T> and IComparer
Dictionary<K,T> System.Collections.Generic HashTable Implements IDictionary<K,T>
EqualityComparer<T> System.Collections.Generic - Abstract class implementing IEqualityComparer<T>
ICollection<T> System.Collections.Generic ICollection Count and synchronization for a collection
IComparer<T> System.Collections.Generic IComparer Compares two specified values
IDictionary<K,T> System.Collections.Generic IDictionary Interface for a collection of key/value pairs
IEnumerable<T> System.Collections.Generic IEnumerable Returns an IEnumerator<T> object
IEnumerator<T> System.Collections.Generic IEnumerator Iterating over a collection
IEqualityComparer<T> System.Collections.Generic IEqualityComparer

(.NET 2.0 only)

Equates two specified values.
IList<T> System.Collections.Generic IList Implemented by list collections or access by index
KeyedCollection<K,T> System.Collections.ObjectModel - Base class for keyed collections
KeyValuePair<K,V> System.Collections.Generic - Container for key/value pair
LinkedList<T> System.Collections.Generic - A true linked list
LinkedListNode<T> System.Collections.Generic - Used by LinkedList<T>, but can be used by custom lists as well.
List<T> System.Collections.Generic ArrayList Impalements IList<T> over an array
Queue<T> System.Collections.Generic Queue A queue
ReadOnlyCollection<T> System.Collections.ObjectModel ReadOnlyCollectionBase Base class for read-only collections
SortedDictionary<K,T> System.Collections.Generic SortedList Implements IDictionary<K,T> over a sorted collection
SortedList<T> System.Collections.Generic SortedList A sorted linked list over an array and a hash table.
Stack<T> System.Collections.Generic Stack A stack

What Are the Generic Delegates?

The System namespace defines five new generic delegates. The first is EventHandler<E> defined as:

[C#]

public delegate void EventHandler<E>(object sender,E e) where E : EventArgs

[Visual Basic]

Public Delegate Sub EventHandler(Of E As EventArgs) _
    (ByVal sender As Object, ByVal e As E)

[Visual C++]

generic <typename T> where E : EventArgs
public delegate void EventHandler (Object ^sender, T e); 

EventHandler<E> can be used wherever an event handling method expects an object and an EventArgs-derived class as parameters. Obviously, that is the case wherever the non-generic EventHandler was used in .NET 1.1:

[C#]

public delegate void EventHandler(object sender, EventArgs e)

[Visual Basic]

Public Delegate Sub EventHandler(ByVal sender As Object, ByVal e As EventArgs)

[Visual C++]

public delegate void EventHandler(Object ^sender, EventArgs ^e);

But in addition, EventHandler<E> can be employed instead of all the other delegates that used EventArgs-derive class, such as MouseEventHandler:

[C#]

public class MouseEventArgs : EventArgs
{...}
public delegate void MouseEventHandler(object sender,MouseEventArgs e);

void OnMyMouseEvent(object sender,MouseEventArgs e)
{...}

//Instead of:
MouseEventHandler handler += OnMyMouseEvent;

//You can write:
EventHandler<MouseEventArgs> handler += OnMyMouseEvent;

[Visual Basic]

Public Class MouseEventArgs
    Inherits EventArgs
    ...
End Class
Public Delegate Sub MouseEventHandler(ByVal sender As Object, ByVal e As MouseEventArgs)

' Instead of:
Public Class SomeClass
   Event handler As MouseEventHandler

   Public Sub SomeMethod()
      AddHandler handler, AddressOf OnMyMouseEvent
   End Sub

   Sub OnMyMouseEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
      ...
   End Sub

End Class

' You can write:
Public Class SomeClass
   Event handler As EventHandler(Of MouseEventArgs)

   Public Sub SomeMethod()
      AddHandler handler, AddressOf OnMyMouseEvent
   End Sub

   Sub OnMyMouseEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
      ...
   End Sub

End Class

[Visual C++]

public ref class MouseEventArgs : EventArgs
{...};
public delegate void MouseEventHandler(Object ^sender, MouseEventArgs ^e);

void OnMyMouseEvent(Object ^sender,MouseEventArgs ^e)
{...}

//Instead of:
MouseEventHandler ^handler += gcnew MouseEventHandler(this, &<ClassName>::OnMyMouseEvent);

//You can write:
EventHandler<MouseEventArgs ^> ^handler += gcnew EvenHandler<MouseEventArgs ^>(this, &<ClassName>::OnMyMouseEvent);

The other four generic delegates found in the System namespace are designed to be used in conjunction with the static generic methods of Array or the List<T> type, but you can easily use them in other contexts:

[C#]

public delegate void Action<T>(T t);
public delegate int Comparison<T>(T x, T y);
public delegate U Converter<T, U>(T from);
public delegate bool Predicate<T>(T t);

[Visual Basic]

Public Delegate Sub Action(Of T)(ByVal t As T)
Public Delegate Function Comparison(Of T)(ByVal x As T, _
    ByVal y As T) As Integer
Public Delegate Function Converter(Of T, U)(ByVal from As T) As U
Public Delegate Function Predicate(Of T)(ByVal t As T) As Boolean

[Visual C++]

generic <typename T>

public delegate void Action(T t);

generic <typename T>

public delegate int Comparison(T x, T y);

generic <typename T, typename U>

public delegate U Converter(T from);

generic <typename T>

public delegate bool Predicate(T t);

For example, here is using the Action<T> delegate to trace every value in a given array:

[C#]

string[] cities = {"New York","San Francisico","London"};

Action<string> trace = delegate(string text)
    {
        Trace.WriteLine(text);
    };
Array.ForEach(cities,trace);

[Visual Basic]

Sub TraceString(ByVal text As String)
   Trace.WriteLine(text)
End Sub

Dim cities() As String = {"New York", "San Francisico", "London"}
Dim actionDelegate As Action(Of String) = AddressOf TraceString
 
Array.ForEach(cities, actionDelegate)

[Visual C++]

void TraceString(String ^text)
{
   Trace::WriteLine(text);
}

array <String ^> ^cities = {"New York","San Francisico","London"};

Action<String ^> ^trace = gcnew Action<String ^>(this, &<ClassName>::TraceString);
Array::ForEach(cities,trace);

What Are the Generic Methods of System.Array?

The System.Array type is extended with many generic static methods. The generic static methods are designed to automate and streamline common tasks of working with arrays, such as iterating over the array and performing an action on each element, scanning the array looking for a value that matches a certain criteria (a predicate), converting and sorting the array, and so on. Below is a partial listing of these static methods:

[C#]

public abstract class Array
{
   //Partial listing of the static methods:  
   public static ReadOnlyCollection<T> AsReadOnly<T>(T[] array);
   public static int BinarySearch<T>(T[] array,T value);
   public static int BinarySearch<T>(T[] array,T value,
                                     IComparer<T> comparer);
   public static U[] ConvertAll<T,U>(T[] array, 
                                Converter<T,U> converter);
   public static bool Exists<T>(T[] array,Predicate<T> match);
   public static T Find<T>(T[] array,Predicate<T> match);
   public static T[] FindAll<T>(T[] array,Predicate<T> match);
   public static int FindIndex<T>(T[] array,Predicate<T> match);
   public static void ForEach<T>(T[] array,Action<T> action);
   public static int  IndexOf<T>(T[] array,T value);
   public static void Sort<T>(T[] array,IComparer<T> comparer);
   public static void Sort<T>(T[] array,Comparison<T> comparison); 
}

[Visual Basic]

Public MustInherit Class Array
    'Partial listing of the shared methods:
    Public Shared Function AsReadOnly(Of T)(ByVal array As T()) 
                                            As ReadOnlyCollection(Of T)
    Public Shared Function BinarySearch(Of T)(ByVal array As T(), _
                                              ByVal value As T) As Integer
    Public Shared Function BinarySearch(Of T)(ByVal array As T(), _
                                              ByVal value As T, _
                                              ByVal comparer As IComparer(Of T)) _
                                              As Integer
    Public Shared Function ConvertAll(Of T, U)(ByVal array As T(), _
                                               ByVal converter As 
                                                     Converter(Of T, U)) As U()
    Public Shared Function Exists(Of T)(ByVal array As T(), _
                                        ByVal match As Predicate(Of T)) As Boolean
    Public Shared Function Find(Of T)(ByVal array As T(), _
                                      ByVal match As Predicate(Of T)) As T
    Public Shared Function FindAll(Of T)(ByVal array As T(), _
                                         ByVal match As Predicate(Of T)) As T()
    Public Shared Function FindIndex(Of T)(ByVal array As T(), _
                                           ByVal match As Predicate(Of T)) _
                                           As Integer
    Public Shared Sub ForEach(Of T)(ByVal array As T(), _
                                    ByVal action As Action(Of T))
    Public Shared Function IndexOf(Of T)(ByVal array As T(), ByVal value As T) _
                                         As Integer
    Public Shared Sub Sort(Of T)(ByVal array As T(),_
                                    ByVal comparer As IComparer(Of T))
    Public Shared Sub Sort(Of T)(ByVal array As T(), _
                                 ByVal comparison As Comparison(Of T))
End Class

[Visual C++]

public ref class Array abstract
{
   //Partial listing of the static methods:  
public: 
   generic <typename T>
   static ReadOnlyCollection<T> ^ AsReadOnly(array<T> ^arr);
   generic <typename T>
   static int BinarySearch (array<T> ^arr, T value);
   generic <typename T>
   static int BinarySearch (array<T> ^arr, T value,
                                     IComparer<T> ^comparer);
   generic <typename T, typename U>
   static array<U> ^ ConvertAll (array<T> ^arr, 
                                Converter<T,U> ^converter);
   generic <typename T>
   static bool Exists (array<T> ^arr,Predicate<T> ^match);
   generic <typename T>
   static T Find (array<T> ^arr,Predicate<T> ^match);
   generic <typename T>
   static array<T> ^ FindAll (array<T> ^arr, Predicate<T> ^match);
   generic <typename T>
   static int FindIndex (array<T> ^arr, Predicate<T> ^match);
   generic <typename T>
   static void ForEach (array<T> ^arr, Action<T> ^action);
   generic <typename T>
   static int  IndexOf (array<T> ^arr, T value);
   generic <typename T>
   static void Sort (array<T> ^arr,IComparer<T> ^comparer);
   generic <typename T>
   static void Sort (array<T> ^arr, Comparison<T> ^comparison) ; 
};

Most of these static generic methods work with the four generic delegates defined in the System namespace:

[C#]

public delegate void Action<T>(T t);
public delegate int Comparison<T>(T x, T y);
public delegate U Converter<T, U>(T from);
public delegate bool Predicate<T>(T t);

[Visual Basic]

Public Delegate Sub Action(Of T)(ByVal t As T)
Public Delegate Function Comparison(Of T)(ByVal x As T, ByVal y As T) As Integer
Public Delegate Function Converter(Of T, U)(ByVal from As T) As U
Public Delegate Function Predicate(Of T)(ByVal t As T) As Boolean

[Visual C++]

generic <typename T> 
public delegate void Action(T t);
generic <typename T>
public delegate int Comparison(T x, T y);
generic <typename T, typename U>
public delegate U Converter(T from);
generic <typename T>
public delegate bool Predicate(T t);

For example, suppose the array roles contains all the roles a user plays at your application, and you would like to find out if the user is a member or a specified role.

[C#]

bool IsInRole(string role)
{
   string[] roles = GetRoles(); 

   Predicate<string> exists = delegate(string roleToMatch)
                              {
                                  return roleToMatch == role;
                              };
   return Array.Exists(roles,exists);
}
string[] GetRoles()
{...}

[Visual Basic]

Public Class SomeClass
   Dim m_RoleToMatch As String

   Private Function CompareRoles(ByVal role As String) As Boolean
      Return role = m_RoleToMatch
   End Function

   Public Function IsInRole(ByVal role As String) As Boolean
      Dim roles As String() = GetRoles()
      m_RoleToMatch = role
      Dim exists As Predicate(Of String) 
      exists = New Predicate(Of String)(AddressOf CompareRoles)
      Return Array.Exists(roles, exists)
   End Function

   Private Function GetRoles() As String()
      ...
   End Function

End Class 

The Array.Exists() method defined as:

[C#]

public static bool Exists<T>(T[] array,Predicate<T> match);

[Visual Basic]

Public Shared Function Exists(Of T)(ByVal array As T(), _
                ByVal match As Predicate(Of T)) As Boolean

[Visual C++]

public: generic<typename T>
static bool Exists(array<T>^ array,Predicate<T>^ match);

takes a single type parameter (the type of the array). The compiler can infer the type automatically, so there is no need to specify that. The second parameter is a generic delegate of type Predicate<T>(), which returns a Boolean value. The Array.Exists() method iterates over the array, and invokes the predicate delegate on each item in the array. If the predicate returns true, it stops the iteration and returns true. If all the items in the array return false from invoking the predicate on them, Array.Exists() returns false. In C#, you can initialize the predicate using an anonymous method, and have Array.Exists() invoke that method on every item in the array until the predicate is satisfied or there are no more items.

To demystify how those various methods work, here is how Array.Exist() could be implemented:

[C#]

public abstract class Array
{
   public static bool Exists<T>(T[] array,Predicate<T> match)
   {
      if(array == null)
      {
         throw new ArgumentNullException("array");
      }
      if(match == null)
      {
         throw new ArgumentNullException("match");
      }
      foreach(T t in array)
      {
         if(match(t))
         {
            return true;
         }
      }
      return false; 
   }
   //Rest of the methods 
}

[Visual Basic]

Public MustInherit Class Array
    Public Shared Function Exists(Of T)(ByVal array As T(), _
                                        ByVal match As Predicate(Of T)) As Boolean
        If array Is Nothing Then Throw New ArgumentNullException("array")
        If match Is Nothing Then Throw New ArgumentNullException("match")
        For Each t As T In array
            If match(t) Then Return True
        Next t
        Return False
        ' Rest of the methods
    End Function
End Class

What Are the Generic Methods of List<T>?

Besides implementing IList<T>, the List<T> type contains many generic helper methods. These methods are designed to automate and streamline common tasks of working with the list, such as iterating over the list and performing a task on each element, scanning the list looking for a value that matches a certain criteria (a predicate), or just searching for a particular value, converting and sorting the list, and so on. Below is a partial listing of these generic methods:

[C#]

public class List<T> : IList<T>, 
{
   //Partial listing of the generic helper methods:  
   public List<U> ConvertAll<U>(Converter<T,U> converter);
   public bool Exists(Predicate<T> match);
   public T Find(Predicate<T> match);
   public List<T> FindAll(Predicate<T> match);
   public int FindIndex(Predicate<T> match);
   public T FindLast(Predicate<T> match);
   public void ForEach(Action<T> action);
   public int LastIndexOf(T item);
   public void Sort(Comparison<T> comparison);
   public T[] ToArray();
   //More members
}

[Visual Basic]

Public Class List(Of T)
    Implements IList(Of T)
    ' Partial listing of the generic helper methods:
    Public Function ConvertAll(Of U)( _
        ByVal converter As Converter(Of T, U)) _
        As List(Of U)
    Public Function Exists(ByVal match As Predicate(Of T)) As Boolean
    Public Function Find(ByVal match As Predicate(Of T)) As T
    Public Function FindAll(ByVal match As Predicate(Of T)) As List(Of T)
    Public Function FindIndex(ByVal match As Predicate(Of T)) As Integer
    Public Function FindLast(ByVal match As Predicate(Of T)) As T
    Public Sub ForEach(ByVal action As Action(Of T))
    Public Function LastIndexOf(ByVal item As T) As Integer
    Public Sub Sort(ByVal comparison As Comparison(Of T))
    Public Function ToArray() As T()
    ' More members
End Class

[Visual C++]

generic <typename T>
public ref class List : IList<T>, ICollection<T>, IEnumerable<T>, 
               IList, ICollection, IEnumerable
{
   //Partial listing of the geenric helper methods:  
public: 
   generic <typename T, typename U>      
   List<U> ^ ConvertAll (Converter<T,U> ^converter);
   bool Exists(Predicate<T> ^match);
   T Find(Predicate<T> ^match);
   List<T> ^ FindAll(Predicate<T> ^match);
   int FindIndex(Predicate<T> ^match);
   T FindLast(Predicate<T> ^match);
   void ForEach(Action<T> ^action);
   int LastIndexOf(T item);
   void Sort(Comparison<T> ^comparison);
   array <T> ^ ToArray();
   //More members
};

Most of these helper generic methods work with the four generic delegates defined in the System namespace:

[C#]

public delegate void Action<T>(T t);
public delegate int Comparison<T>(T x, T y);
public delegate U Converter<T, U>(T from);
public delegate bool Predicate<T>(T t);

[Visual Basic]

Public Delegate Sub Action(Of T)(ByVal t As T)
Public Delegate Function Comparison(Of T)(ByVal x As T, _
    ByVal y As T) As Integer
Public Delegate Function Converter(Of T, U)(ByVal from As T) As U
Public Delegate Function Predicate(Of T)(ByVal t As T) As Boolean

[Visual C++]

generic <typename T>
public delegate void Action(T t);
generic <typename T>
public delegate int Comparison(T x, T y);
generic <typename T, typename U>
public delegate U Converter(T from);
generic <typename T>
public delegate bool Predicate(T t);

The List<T> helper methods are used much the same way as the generic static methods of System.Array. For example, the following code initializes a list with all the numbers from 1 to 20. Then, using the Action<T> delegate, the code traces these numbers using the List<T>.ForEach() method. Using the Predicate<T> delegate, the code finds all the prime numbers in the list by calling the List<T>.FindAll() method, which returns another list of the same type. Finally, the prime numbers are traced, using the same Action<T> delegate.

[C#]

int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
List<int> list = new List<int>(numbers);

Action<int> trace =  delegate(int number)
                     {
                        Trace.WriteLine(number);
                     };
Predicate<int> isPrime =   delegate(int number)
                           {
                              switch(number)
                              {
                                 case 1:case 2:case 3:case 5:case 7:
                                 case 11:case 13:case 17:case 19:
                                    return true;
                                 default:
                                    return false;
                              }
                           };
list.ForEach(trace);
List<int> primes = list.FindAll(isPrime);
primes.ForEach(trace);

[Visual Basic]

Sub TraceNumber(ByVal number As Integer)
   Trace.WriteLine(number)
End Sub

Function IsPrimeNumber(ByVal number As Integer) As Boolean
   Select Case number
      Case 1,2,3,5,7,11,13,17,19
         Return True
      Case Else
         Return False
   End Select
End Function

Dim numbers() As Integer = _
  {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
   Dim list As New List(Of Integer)(numbers)

   Dim trace As Action(Of Integer)
   trace = New Action(Of Integer)(AddressOf TraceNumber)

   Dim isPrime = New Predicate(Of Integer)(AddressOf IsPrimeNumber)
   list.ForEach(trace)
   
   Dim primes As List(Of Integer) = list.FindAll(isPrime)
   primes.ForEach(trace)
End Sub

[Visual C++]

Bool IsPrimeNumber(int number)
{
   switch(number)
   {
      case 1:case 2:case 3:case 5:case 7:
      case 11:case 13:case 17:case 19:
         return true;
      default:
         return false;
   }
}
void TraceNumber(int number)
{
   Trace::WriteLine(number);
}

int numbers[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
List<int> ^list = gcnew List<int>(numbers);

Action<int> ^trace = gcnew Action<int>(this,&<ClassName>::TraceNumber);
Predicate<int> ^isPrime =  gcnew Predicate<int>(this,&<ClassName>::IsPrimeNumber);

list->ForEach(trace);
List<int> ^primes = list->FindAll(isPrime);
primes->ForEach(trace);

What Are Nullable Types?

Unlike reference types, you cannot assign a null into a value type. This is often a problem when interacting with code that interprets a null as having no value, rather than no-reference. The canonical example is database null values in columns that have representation as types such as int or DateTime. To address that, the System namespace provides the structure Nullable<T> defined as:

[C#]

public interface INullableValue
{
   bool HasValue{get;}
   object Value{get;}
}
[Serializable]
public struct Nullable<T> : INullableValue,IEquatable<Nullable<T>>,... where T : struct
{
   public Nullable(T value);
   public bool HasValue{get;}
   public T Value{get;}
   public T GetValueOrDefault();
   public T GetValueOrDefault(T defaultValue);
   public bool Equals(Nullable<T> other);   
   public static implicit operator Nullable<T>(T value);
   public static explicit operator T(Nullable<T> value);

   //More members
}

[Visual Basic]

Public Interface INullableValue
    ReadOnly Property HasValue() As Boolean
    ReadOnly Property Value() As Object
End Interface

Public Structure Nullable(Of T As Structure)
    Implements INullableValue,IEquatable(Of Nullable(Of T)), ' More interfaces 

    Public Sub New(value As T)
    Public ReadOnly Property HasValue() As Boolean
    Public ReadOnly Property Value() As T
    Public Function GetValueOrDefault() As T
    Public Function GetValueOrDefault(ByVal defaultValue As T) As T
    Public Function Equals(ByVal other As Nullable(Of T)) As Boolean
    Public Shared Operator CType(ByVal value As T) As Nullable(Of T)
    Public Shared Operator CType(ByVal value As Nullable(Of T)) As T
    ' More members
End Structure

[Visual C++]

public interface class INullableValue
{
   property bool HasValue{ bool get();}
   property Object ^ Value{ Object ^get();}
}
generic <typename T>
[Serializable]
public value struct Nullable : INullableValue,IEquatable<Nullable<T>>...
{
public: 
   Nullable(T value);
   property bool HasValue { bool get(); }
   property T Value { T get(); }
   bool Equals(Nullable<T> other);
   T GetValueOrDefault();
   T GetValueOrDefault(T defaultValue);
   generic <typename T>
   static operator Nullable(T value);
   static explicit operator T(Nullable<T> value);
   //More members
};

Because the Nullable<T> struct uses a generic type parameter, you can use it to wrap a value type, and assign null into it:

[C#]

Nullable<int> number = 123;
Debug.Assert(number.HasValue);
number = null;
Debug.Assert(number.HasValue == false);
Debug.Assert(number.Equals(null));

[Visual Basic]

Dim number As Nullable(Of Integer) = 123
Debug.Assert(number.HasValue())
number = Nothing
Debug.Assert(number.HasValue() = False)
Debug.Assert(number.Equals(Nothing))

[Visual C++]

Nullable<int> number = 123;
Debug::Assert(number.HasValue);
number = Nullable<int>::FromObject((Object ^)nullptr);
Debug::Assert(number.HasValue == false);
Debug::Assert(number.Equals(null));

Once a null is assigned to a nullable type, you can still access it to verify if it has a value, via the HasValue property, or just equate it to null.

In C# and Visual Basic, you can even use the underlying value type's operators on a nullable type:

[C#]

Nullable<int> number = 0;
number++;

[Visual Basic]

Dim number As Nullable(Of Integer) = 0
number += 1

The reason this is possible is because the compiler is capable of verifying that the underlying type supported the operator, and applying it on the value stored in the structure. This is called lifted operators.

The Nullable<T> struct also provides conversion operators, so you can convert a nullable type to and from a real value type:

[C#]

Nullable<int> nullableNumber = 123;
int number = (int)nullableNumber;
Debug.Assert(number == 123);

number = 456;
nullableNumber = number;
Debug.Assert(nullableNumber.Equals(456));

[Visual Basic]

Dim nullableNumber As Nullable(Of Integer) = 123
Dim number As Integer = CType(nullableNumber, Integer)
Debug.Assert(number = 123)
      
number = 456
nullableNumber = number
Debug.Assert(nullableNumber.Equals(456))

[Visual C++]

Nullable<int> nullableNumber = 123;
int number = (int)nullableNumber
Debug::Assert(number == 123);

number = 456;
nullableNumber = number;
Debug::Assert(nullableNumber.Equals(456));

Note that using Nullable<T> on Nullable<T> is disallowed, and the compiler will issue an error:

[C#]

//This will not compile:
Nullable<Nullable<int>> number = 123;

[Visual Basic]

' This will not compile:
Dim number As Nullable(Of Nullable(Of Integer)) = 123

[Visual C++]

//This will not compile:
Nullable<Nullable<int>> number = 123;

You can use the overloaded methods GetValueOrDefault() of Nullable<T> to defensively obtain either the value stored in the nullable type or it its default, if it does contain a null:

[C#]

Nullable<DateTime> time = null;
DateTime value = time.GetValueOrDefault();
Debug.Assert(value.ToString() == "1/1/0001 12:00:00 AM");

[Visual Basic]

Dim time As Nullable(Of DateTime)
Dim value As DateTime = time.GetValueOrDefault()
Debug.Assert(value.ToString() = "1/1/0001 12:00:00 AM")

[Visual C++]

Nullable<DateTime> time = null;
DateTime value = time.GetValueOrDefault();
Debug.Assert(value.ToString() == "1/1/0001 12:00:00 AM");

The System namespace also defines the static helper class Nullable and the helper class NullableConverter, but those are not needed usually.

The C# 2.0 compiler supports shorthand for Nullable<T>. You can use the ? modifier on value types to actually construct a Nullable<T> around it:

int? number = 123;
Debug.Assert(number.HasValue);
number = null;
Debug.Assert(number.HasValue == false);

Note that the type declared by the ? modifier is identical to that created using Nullable<T> directly:

Debug.Assert(typeof(int?) == typeof(Nullable<int>));

As with using Nullable<T> directly, the compiler supports lifted operators. Whenever you combine nullable types using operators, if any one of them is null, then the resulting expression will be null too:

int? number1 = 123;
int? number2 = null;
int? sum = number1 + number2;
Debug.Assert(sum == null);

Using the ? modifier is the common way of declaring and using nullable variables in C#. You can even pass nullable types as type arguments for generic types:

IList<int?> list = new List<int?>();
list.Add(3);
list.Add(null);

C# 2.0 also provides the null coalescing operator via the ?? operator.

c = a ?? b;

The result of applying the ?? operator on two operands returns the left hand side operand (a) if it is not null, and the right operand (b)otherwise. While b can of course be null too, you typically use the ?? operator to supply a default value in case a is null.

How Do I Reflect Generic Types?

Like most other things done with reflection, you use the class Type. Type can represent generic types with specific type arguments (called bounded types), or unspecified (unbounded) types.

[C#]

Both typeof and GetType() can operate on type parameters:

public class MyClass<T> 
{
   public void SomeMethod(T t)
   {
      Type type = typeof(T);
      Debug.Assert(type == t.GetType());
   }
}

In addition the typeof operator can operate on unbound generic types (generic types that do not have yet specific type arguments). For example:

public class MyClass<T> 
{}
Type unboundedType = typeof(MyClass<>);

Trace.WriteLine(unboundedType.ToString());
//Writes: MyClass`1[T]

The number 1 being traced is the number of generic type parameters of the generic type used. Note the use of the empty <>. To operate on an unbound generic type with multiple type parameters, use a , in the <>:

public class LinkedList<K,T> 
{...}
Type unboundedList = typeof(LinkedList<,>);

Trace.WriteLine(unboundedList.ToString());
//Writes: LinkedList`2[K,T]

[Visual Basic]

Both GetType() and Object.GetType() can operate on type parameters:

Public Class SomeClass(Of T)
   Public Sub SomeMethod(ByVal t As T)
      Dim theType As Type = GetType(T)
      Debug.Assert((theType Is t.GetType))
   End Sub
End Class

[Visual C++]

Both typeid<> and GetType() can operate on type parameters:

generic <typename T>
public ref class MyClass 
{
public: 
   void SomeMethod(T t)
   {
      Type ^type = typeid<T>;
      Debug::Assert(type == t->GetType());
   }
};

To support generics, Type has special methods and properties designed to provide reflection information about the generic aspects of the type:

[C#]

public abstract class Type : //Base types
{
   public virtual bool ContainsGenericParameters{get;}
   public virtual GenericParameterAttributes 
      GenericParameterAttributes{get;}
   public virtual int GenericParameterPosition{get;}
   public virtual bool IsGenericType{get;}
   public virtual bool IsGenericParameter{get;}
   public virtual bool IsGenericTypeDefinition{get;}
   public virtual Type[] GetGenericArguments();
   public virtual Type[] GetGenericParameterConstraints();
   public virtual Type GetGenericTypeDefinition();
   public virtual Type MakeGenericType(params Type[] typeArguments);
   //Rest of the members
}

[Visual Basic]

Public MustInherit Class Type ' Base types
   Public Overridable ReadOnly Property ContainsGenericParameters _
      As Boolean
   Public Overridable ReadOnly Property GenericParameterAttributes _
      As GenericParameterAttributes
   Public Overridable ReadOnly Property GenericParameterPosition _
      As Integer
   Public Overridable ReadOnly Property IsGenericType As Boolean
   Public Overridable ReadOnly Property IsGenericParameter As Boolean
   Public Overridable ReadOnly Property IsGenericTypeDefinition As Boolean
   Public Overridable Function GetGenericArguments() As Type()
   Public Overridable Function GetGenericParameterConstraints() As Type()
   Public Overridable Function GetGenericTypeDefinition() As Type
   Public Overridable Function MakeGenericType(ByVal ParamArray _
      typeArguments As Type()) As Type
   ' Rest of the members
End Class

[Visual C++]

public ref class Type abstract : //Base types
{
public: 
   property virtual GenericParameterAttributes GenericParameterAttributes{ 
                                       GenericParameterAttributes get;}
   property virtual bool ContainsGenericParameters{ bool get();}
   property virtual int GenericParameterPosition{ int get();}
   property virtual bool IsGenericType{ bool get();}
   property virtual bool IsGenericParameter{bool get();}
   property virtual bool IsGenericTypeDefinition{ bool get();}
   virtual array<Type ^> ^ GetGenericArguments();
   virtual array<Type^>^ GetGenericParameterConstraints();
   virtual Type ^ GetGenericTypeDefinition();
   virtual Type^ MakeGenericType(... array<Type^>^ typeArguments);
   //Rest of the members
};

The most useful of these new members are the IsGenericType property, the GetGenericArguments() and GetGenericTypeDefinition() methods. As its name indicates, IsGenericType is set to true if the type represented by the Type object uses generic type parameters. GetGenericArguments() returns an array of types corresponding to the type arguments used. GetGenericTypeDefinition() returns a Type representing the generic form of the underlying type. The following example demonstrates using these generic-handling Type members to obtain generic reflection information on a generic linked list.

[C#]

public class LinkedList<K,T>
{...}

LinkedList<int,string> list = new LinkedList<int,string>();

Type boundedType = list.GetType();
Trace.WriteLine(boundedType.ToString());
//Writes: LinkedList`2[System.Int32,System.String]

Debug.Assert(boundedType.IsGenericType);

Type[] parameters = boundedType.GetGenericArguments();

Debug.Assert(parameters.Length == 2);
Debug.Assert(parameters[0] == typeof(int));
Debug.Assert(parameters[1] == typeof(string));

Type unboundedType = boundedType.GetGenericTypeDefinition();
Trace.WriteLine(unboundedType.ToString());
//Writes: LinkedList`2[K,T]

[Visual Basic]

Class LinkedList(Of T, K)
   ...
End Class

Dim list As New LinkedList(Of Integer, String)
Dim listType As Type = list.GetType()
Trace.WriteLine(listType.ToString)
' Writes: LinkedList`2[System.Int32,System.String]

Debug.Assert(listType.IsGenericType)

Dim parameters As Type() = listType.GetGenericArguments()

Debug.Assert(parameters.Length = 2)
Debug.Assert(parameters(0) Is GetType(Integer))
Debug.Assert(parameters(1) Is GetType(String))

Dim unboundedType As Type = listType.GetGenericTypeDefinition()
Trace.WriteLine(unboundedType.ToString)
' Writes: LinkedList`2[K,T]

[Visual C++]

generic <typename K, typename T>
public ref class LinkedList
{...};

LinkedList<int,String ^> ^list = gcnew LinkedList<int,String ^>;

Type ^boundedType = list->GetType();
Trace::WriteLine(boundedType->ToString());
//Writes: LinkedList`2[System.Int32,System.String]

Debug::Assert(boundedType->IsGenericType);

array <Type ^> ^parameters = boundedType->GetGenericArguments();

Debug::Assert(parameters->Length == 2);
Debug::Assert(parameters[0] == typeid<int>);
Debug::Assert(parameters[1] == typeid<String ^>);

Type ^unboundedType = boundedType->GetGenericTypeDefinition();
Trace::WriteLine(unboundedType->ToString());
//Writes: LinkedList`2[K,T]

 

About the author

Juval Lowy is a software architect and the principal of IDesign, specializing in .NET architecture consulting and advanced .NET training. Juval is Microsoft's Regional Director for the Silicon Valley, working with Microsoft on helping the industry adopt .NET. His latest book is Programming .NET Components 2nd Edition (O'Reilly, 2005). Juval participates in the Microsoft internal design reviews for future versions of .NET. Juval published numerous articles, regarding almost every aspect of .NET development, and is a frequent presenter at development conferences. Microsoft recognized Juval as a Software Legend as one of the world's top .NET experts and industry leaders.

Show:
© 2014 Microsoft. All rights reserved.