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

260
Visualizações
How to check if parameters are null when using positional record constructors and/or init properties?

I would like check no null parameters are assigned to record fields and let the properties have comments. I have found out the following arrangement does the trick for comments, but I lack ideas to check for null parameters without turning this record into a class.

So, a question: Is it possible to check during runtime that nulls won't be assigned to record fields? If so, how could one do it while still using records?

public record Test(string TestString)
{
   /// <summary>
   /// This is one way to get a comment on record properties. Are there others?
   /// </summary>
   public string TestString { get; init; } = TestString;
}

The code is also as a gist here.

This is an addition, but maybe helps with the accepted answer and the comment of different code.

It was because the code in the gist did a null check like

public record Test(string TestString)
{
   /// <summary>
   /// This is one way to get a comment on record properties. Are there others?
   /// </summary>
   public string TestString { get; init; } = TestString ?? throw new ArgumentNullException();
}

For a reason or another, I didn't include this pondering in the original question if there's maybe a shorter way. :)

<edit: The .NET 6 ArgumentNullException.ThrowIfNull(obj) could be handy here (or ThrowHelper).

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

0

You have to handle checks during inline assignment, as you've already discovered in your gist.

One possibility is to call an extension method to wrap any behavior.

public record Test(string TestString)
{
    /// <summary>
    /// This is one way to get a comment on record properties. Are there others?
    /// </summary>
    public string TestString { get; init; } = ValidationExtensions.Validate(TestString);
}

public static class ValidationExtensions
{
    public static string Validate(string input)
    {
        if (string.IsNullOrEmpty(input))
            throw new NullReferenceException();
        
        return input;
    }
}

This will correctly throw during initialization of the record:

var x = new Test(null);
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