The restriction on T in this case means that T must implement AppBase<T> and have a default constructor. In fact, you need to pass yourself as a type.
This is usually done as an attempt to bypass the type system and provide access to the implementation type in the base class via typeof(T) . For example, given:
public interface IApp {} public abstract class AppBase<T> : IApp where T : AppBase<T>, new() { public void Print() { Console.WriteLine(typeof(T).ToString()); } } public class AppBaseFoo : AppBase<AppBaseFoo> { }
Then you can write code, for example:
var foo = new AppBaseFoo(); foo.Print();
Type information for AppBaseFoo will be printed. However, this is not reliable - for example, subclasses βbreakβ it. Addendum:
public class AppBaseBar : AppBaseFoo {}
And then I write:
var bar = new AppBaseFoo(); bar.Print();
Invokes printing of the same AppBaseFoo information.
Reed copsey
source share