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

161
Visualizações
Better solution to allow the MinValue and MaxValue fields from a Generic type in C#

I have the following code and I want to pass the MinValue and MaxValue of a generic type. It would also help to somehow be able to specify that the generic type should have the MinValue and MaxValue properties via interfaces but I cannot not find the right interface.

public class TreeNode<T> where T : IComparable
{
    public T data { get; set; }

    public TreeNode(T value) {...}
    
    public bool IsValidBST()
    {
        return IsValidBST(this, T.MinValue, T.MaxValue);
    }

    public bool IsValidBST(TreeNode<T> node, T min, T max) {...}
}

But my compiler tells me that "T is a type parameter which is not valid in the given context".

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

0

As of early 2022, this functionality is currently in preview in .NET 6, under the work being done on generic math. The interface in question is IMinMaxValue<TSelf>:

public class TreeNode<T> where T : IMinMaxValue<T>
{
    // ...

    public bool IsValidBST()
    {
        return IsValidBST(this, T.MinValue, T.MaxValue);
    }
}

See it on SharpLab.

However, static abstract members, which let you access e.g. T.MinValue on some generic type T, are not currently available unless you opt into these preview features (see the blog post above for instructions on how to do this).

If you don't want to opt into preview features, there is no built-in functionality to do what you need.

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