Category Archive for: Generics

Other Generic Framework Types

In addition to the System. Collections. Generic namespace, the .NET Framework has other uses for generic types. The structs and delegates discussed here are all in the System namespace and serve different purposes. This section discusses the following:  The struct Nullable<T>  The delegate EventHandler<TEventArgs>  ThestructArraySegment<T> Nullable<T> A number in a database and a number in a programming…

Read More →

Generic Delegates

delegates are type-safe references to methods. With generic delegates, the parameters of the delegate can be defined later. The .NET Framework defines a generic EventHandler delegate with the second parameter of type TEventArgs, so it is no longer necessary to define a new delegate with every new parameter type: public sealed delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)…

Read More →

Generic Methods

In addition to defining generic classes, it is also possible to define generic methods. With a generic method, the generic type is defined with the method declaration. The method Swap<‘:’> defines T as a generic type that is used for two arguments and a variable temp: { T temp; temp = x; x = y; y…

Read More →

Generic Interfaces

Using generics, you can define interfaces that define methods with generic parameters. In the linked list sample, you’ve already implemented the interface IEnumerable<T>, which defines a GetEnumerator (~method to return IEnumerator<T>. For many non-generic interfaces of .NET 1.0, new generic versions have been defined since .NET 2.0, for example IComparable<T>: Img the non-generic interface IComparable that requires an…

Read More →

Inheritance

A generic type can implement a generic interface, The same is possible by deriving from a class. A generic class can be derived from a generic base class: Img The requirement is that the generic types of the interface must be repeated. or the type of the base class must be specified. as in this case:…

Read More →

Generic Classes Features

When creating generic classes, you might need some more C# keywords. For example, it is not possible to assign null to a generic type. In this case, the keyword default can be used. If the generic type.does not require the features of the Object class, but you need to invoke some specific methods in the generic class, you…

Read More →

Creating Generic Classes

First start with a normal, non-generic simplified linked list class that can contain objects of any kind, and later convert this class to a generic class. With a linked list, one element references the next one. So, you must create a class that wraps the object inside the linked list and references the next object. The class…

Read More →

Overview

Generics are not a completely new construct; similar concepts exist with other languages. For example, C++ templates can be compared to generics. However, there’s a big difference between C++ templates and .NET generics. With C++ templates, the source code of the template is required. when a template is instantiated with a specific type. Contrary to C++ templates, generics…

Read More →

Generics

A new feature of the CLR 2.0 is the introduction of generics. With CLR 1.0, creating a flexible class or method that should use classes that are not known at compile time must be based on the Object. class. With the Object class, there’s no type safety during compile time. Casting is necessary. Also, using the Object class…

Read More →

Back to Top