C#: Generics

Please see my other C# articles.

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 )

Facebook photo

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

Connecting to %s