Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
How to understand recursive generic type constraint in Unity C#?

While I understand generic types <T> and where clause for constraint, I was confused about the following code from Unity Tower Defense Template:

public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>

What's the purpose of restricting the type to be itself?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It prevents you from doing this:

public class A
{
}

public class B : Singleton<A>
{
}

You will get a compile error. Due to the type constraint, you must write:

public class A : Singleton<A>
{
}
over 4 years ago · Santiago Trujillo Denunciar

0

Off the top of my head, one possible benefit of this is allowing methods to return the specific type. This is done by fluent style methods. Another example, obviously not applicable to the example you posted, is a Copy() method that returns a copy of the object as the derived type rather than as its base type. The method signatures of the Singleton< T> class you posted likely show where it uses the T parameter and likely hint at the reason why it uses this pattern.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda