Tuesday, July 8, 2008

Serializing, Consuming, and Binding Generics in .NET 2.0

Generics, a new feature introduced with .NET 2.0, provides type safety at compile time. Generics let you create data structures without committing to a specific data types in your code at design time. At compile time, the compiler ensures that the types used with the data structure are consistent with type safety. In other words, generics provide type safety, but without any loss of performance or code bloat. While they are similar to templates in C++ in this regard, their implementation is very different.This article discusses the application of generics in .NET development and different ways in which you can leverage generic types and collections within a .NET application.What are Generics?Generics are code templates generalized to use the same code repeatedly with different data types without needing to rewrite any of the internal code, and thus increasing the reusability of your software components. Within the data structure, member or argument usage adapts to different data types. Generics let you avoid those all-too-common, messy, and resource-intensive conversions from reference types to native types, creating routines that are much more type-safe.You define a generic using a slightly different notation than you're used to with non-generic types. For example, here's the basic code for a generic class named Compare that can compare two items of the same type and return the larger or smaller value, depending on which method is invoked:

public class Compare { public ItemType ReturnLarger (ItemType data, ItemType data2) { //logic... } public ItemType ReturnSmaller (ItemType data, ItemType data2) { //logic... } }

1 comment:

Vijay Singh Bhadoriya said...

The information is godd however, We need more generic defination for generic class....