VS 2022 Version 17.11.2
Roslyn 4.11.0 (5e3a11e2e7)
Project info
C# 12.0
.NET 8.0
Steps to Reproduce
public class C
{
public Make M()
{
return new()
{
Name = 123,
Value =,
}
}
}
public class Make
{
public string Name { get; set; }
public int Value { get; set; }
}
In the above example, 123 cannot be assigned to Name because int cannot be converted to string.
Expected Behavior
An error is reported on 123.
Actual Behavior
No error is reported, and instead only a nullability warning may appear (if nullable is enabled). Despite the nullability annotations, the warning is reported.
Notes
This does not happen if the actual name of the type is provided, like Make. So, based on the above example, if we write
Version Used
VS 2022 Version 17.11.2 Roslyn 4.11.0 (5e3a11e2e7)
Project info C# 12.0 .NET 8.0
Steps to Reproduce
In SharpLab
In the above example,
123
cannot be assigned toName
becauseint
cannot be converted tostring
.Expected Behavior
An error is reported on
123
.Actual Behavior
No error is reported, and instead only a nullability warning may appear (if nullable is enabled). Despite the nullability annotations, the warning is reported.
Notes
This does not happen if the actual name of the type is provided, like
Make
. So, based on the above example, if we writethe error would be correctly reported on
123
.