Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
C# generic class type parameter and constraint that is the same as the generic parameter

The Stripe payment .NET API has a generic class definition like below and I'd like to understand why the constraint part StripeEntity<T> should be the type T.

public abstract class StripeEntity<T> : StripeEntity where T : StripeEntity<T>

From: https://github.com/stripe/stripe-dotnet/blob/master/src/Stripe.net/Entities/_base/StripeEntity.cs

I understand the generic syntax, but don't understand why the class is defined in this way, the thinking behind it. Is this a common pattern around generic classes and if so what are the benefits?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's called the curiously recurring pattern, usually used in C++.

It has a couple of things going for it, especially the fact that it's a (very limited) form of static polymorphism, and so avoids the costs of calling virtual functions. That has more value in C++ than in C# however, due to how the language and runtime are constructed.

.Net also has better ways of dealing with this, for example you could achieve something similar in a cleaner way by using attributes and source generators.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda