C#: 4.0 Generics

Ensuring type-safety in objects.

Whenever performing domain modeling, it’s critical to ensure code matches the real-world environment as much as possible.
The chief method for doing so to utilize classes by which objects may be instantiated.
However, having a collection of objects that are either the incorrect type or contain incorrect data types nullifies the purpose for having such collections.
Therefore, C# was updated to provide a new type, Generics.
They ensure objects stored in a collection are the correct type and also natively only support intended data types.
In the example below taken from the Nested Repeaters example, you’ll see I make use of generics to store a collection of “order” objects.

Generics code

generics-code

Generics class

generics-class

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