Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good introduction to generics

Being compelled by the advantages I'm looking for a way to integrate generic programming into my current programming style. I would like to use generics in C# but can't find any good introductory material with some everyday examples of use. If you have experience with generics: what resources did you find most useful learning them? (books, articles, etc...)

like image 814
akosch Avatar asked Jan 22 '23 23:01

akosch


2 Answers

O'reilly article on generics

And I really enjoyed the generics part of the C# in Depth book by Jon Skeet, although it's not introductory but more ... in depth (as in read it when you're comfortable with generics to know a lot of interesting things about them, but not as an introduction).

like image 152
Jorge Córdoba Avatar answered Feb 04 '23 02:02

Jorge Córdoba


Honestly, I found just using the System.Collections.Generic classes to be the best starting point. If you aren't already, switch away from using the System.Collections classes to the new generic variants. That will get you used to the concepts. A strongly typed dictionary is a lovely thing.

After that it's not too much of a conceptual leap to create your own generic class. Intellisense is an amazing guide. Just start writing:

class Something<T> {
   T Item { get; set; }
}

And notice that your second "T" shows up in intellisense. Visual Studio is cheering you on! Hey, this is easy!

Eventually you'll exhaust the obvious and then will need a better resource. Google and MSDN has been all I've needed to date, but by the time you get beyond that and want a deeper understanding you'll already know enough to find the best books for your level of understanding.

Good luck!

like image 30
roufamatic Avatar answered Feb 04 '23 02:02

roufamatic