I have strange issue with nullable (I think) types in .net
I simplified my issue to this code:
using System.Linq;
namespace CustomAttribute
{
class Program
{
static void Main(string[] args)
{
var person1_firstName_Props = typeof(Person1).GetProperties().Single(x => x.Name == nameof(Person1.FirstName));
var person2_firstName_Props = typeof(Person2).GetProperties().Single(x => x.Name == nameof(Person2.FirstName));
var person1_firstName_CustomAttributes = person1_firstName_Props.CustomAttributes;
var person2_firstName_CustomAttributes = person2_firstName_Props.CustomAttributes;
// why person2_firstName_CustomAttributes is empty? if we changed nullability of LastName ?
}
}
class Person1
{
public string? FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
}
class Person2
{
public string? FirstName { get; set; } = null!;
public string? LastName { get; set; } = null!;
}
}
The question is why there is a change related with FirstName field (its properties/attributes) when I change LastName field feature (I added '?' nullable to it). The issue occures in c# 8 and also in earlier versions.