.Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5…

Follow publication

Member-only story

Discovering generics in C#

Because abstraction can save you lots of code repetitions!

Mina Pêcheux
.Net Programming
Published in
7 min readMar 3, 2023
Photo by Jess Bailey on Unsplash

As a dev, there is quite a common issue you may have to deal with when working on a growing project: code snippets that keep repeating over and over again here and there in the codebase. This happens quite often, because factorising code is hard and requires quite a lot of discipline.

But in C#, there’s actually a great tool for defining data type-specific behaviour in a consistent and efficient way: C# generics.

So, in this tutorial, let’s have a quick look at this cool feature and see how it can help us sanitise our codebase :)

Understanding the basics

When you define a generic method or class, you essentially define an empty model, a blueprint than can then be instantiated and that doesn’t know (or knows only partially) what data type it is working on. From a programmer’s point of view, the unknown data type(s) are labelled and booked in memory, just like a variable:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

.Net Programming
.Net Programming

Published in .Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

Mina Pêcheux
Mina Pêcheux

Written by Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)

No responses yet

Write a response