class GenericClass<T> : SomeBaseClass where T : SomeBaseClass { public T GetT() { return this as T; } } 

Question: This code works, but I do not understand how. If T is a kind of SomeDerrivedClass that inherits from SomeBaseClass, then how does the conversion of this (i.e., GenericClass <T> instance) to type T occur?

UPD: set foot, the GetT () method returns null, yes. The question arose during the reading of this article on Habré: the link to the article There is given such code:

 public class MonoBehaviourSingleton<T> : MonoBehaviour where T : MonoBehaviour { public static T Instance; protected virtual void Awake() { Instance = GetInstance(); } protected T GetInstance() { return this as T; } } 
  • Well, to compile, we will put it, and it will not even fall in runtime, it will simply return null , since this transformation is impossible. - rdorn
  • This code really works or returns null, since Couldn't convert type? - PashaPash
  • This is an attempt to cast, you can try to anything (well, almost anything) - tym32167
  • DerrivedClass does DerrivedClass look DerrivedClass ? - Grundy
  • @Grundy class DerrivedClass: BaseClass {} - Pavlin

0