Home > .NET Framework > CLR Generics – Other interesting trivia

CLR Generics – Other interesting trivia

Default keyword
Inititalizing a generic to a default is not possible in case of value types. In case of reference types it is as easy as "T myVar=null;". In case of value types this is illegal. For value types the C# language has been extended using a new keyword default. "T myVar = default(T);"

Generic Interfaces, Delegates, Methods: In addition to Generic classes there can be Generic interfaces, Generic delegates and Generic methods. Let us just see an example of each.

public interface IEnumerator : IDisposable, IEnumerator {
	T Current { get; }
public delegate void EventHandler (
	object sender, TEventArgs e) where TEventArgs : EventArgs;
public static class Interlocked {
	public static T Exchange( ref T location1, T value) where T : class { ... }
	// Other members and implementation elided for clarity

Properties, Indexers, Events, Operators, Constructors, Finalizers cannot have generic type parameters on their own like the Generic method Exchange defined above with a type parameter T.

Categories: .NET Framework
  1. No comments yet.
  1. November 19, 2010 at 11:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: