C # Generics - trying to find a template name

I am trying to find the name of a generic usage pattern in C #, and I don't know a better resource than a stack overflow!

What I'm doing is creating base base classes that the derived class is directly passed to - I looked and looked for examples of this or the name of the template, and I was hoping one of the great members of this community could help me.

I wrote about how it works at http://www.mobiusbay.com/home/alittlesomethingimcallingcascadinggenerics , but if you prefer not to read it, an example of this follows

public abstract class IndexedMemberBase<TDerivedClass> : IDisposable where TDerivedClass : IndexedMemberBase<TDerivedClass> { #region Declarations & Properties private static List<TDerivedClass> derivedInstances = new List<TDerivedClass>(); public static List<TDerivedClass> DerivedInstances { get { return derivedInstances; } } #endregion #region Constructor(s) public IndexedMemberBase() { if (derivedInstances == null) derivedInstances = new List<TDerivedClass>(); derivedInstances.Add((TDerivedClass)this); } #endregion #region Methods public void Dispose() { if (derivedInstances.Contains(this) == true) { derivedInstances.Remove((TDerivedClass)this); } } #endregion } 

That a base class and a derived class can be incredibly simple - for example:

 public class IndexMember : IndexedMemberBase<IndexMember> { //Add all the crazy business you could ever want! } 

This piece of code throws a pointer to each instance of the dervied class into a static collection for general use - the collection is different for each derived class, so you will not have conflicts with the existing one statically in the database. I believe that this is very useful in my projects, and I hope that someone else will find it useful.

It is important to note that the derived class passes in a common signature to the base class.

If anyone has seen this template in use or knows a name for it, I would really like to see some examples, or at least call it the correct name! At the moment, I call it recursive generics, as it seems to fit, but I'm sure the best name is there!

Many thanks for your help!

Adam

+4
source share
2 answers

He called a curiously repeating pattern template.

(and for some reason is it very famous on SO?)

+4
source

Eric Lippert wrote about this on his blog .

He called the Curiously Recurring Template Pattern, although this applies to C ++, where they are templates, not generics.

+4
source

All Articles