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

243
Visualizações
C# Uri? vs. Nullable<Uri>

I've always known in C# that you can represent nullable types in multiple ways, with the difference often being described as "syntactic sugar". For example, the following...

// Case 1
int? nullableIntegerVar01;
// Case #2 - 
System.Nullable<int> nullableIntegerVar02;
// Case #3 - 
Uri? nullableUriVar01;
// Case #4 
System.Nullable<Uri> nullableUriVar02;

Oddly, case #4 above does not compile and throw the following error.

"Error CS0453 The type 'Uri' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable'"

I understand the error, I do not understand the reasoning - I expected these two methods of defining nullable types to be interchangeable.

Further, the following does not work.

var testType = typeof(Uri);
var testNullableType = typeof(Nullable<>).MakeGenericType(testType);

Can anyone provide insight?

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

0

For value types, such as int or structs (that normally can never be null), x? is equivalent to Nullable<x> (syntactic sugar), and it creates a new type. For reference types such as Uri the ? only serves as an annotation, it doesn't create a new type (these can be null regardless of the presence of the ? marker).

Docs:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

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