-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CS8619 is reported on anonymous types #62500
Comments
The code I used on SharpLab: #nullable enable
using System.Collections.Generic;
using System.Linq;
public static class Program
{
public class MyClass
{
public MyClass(string? value1, string value2)
{
Value1 = value1;
Value2 = value2;
}
public string? Value1 { get; set; }
public string Value2 { get; set; }
}
public static void Main()
{
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass(null, "Value2"));
var list2 = list.Select(x => new
{
x.Value1,
Value2New = x.Value2
});
}
} Note that this does not generate the compiler warning. Unsure as to why. |
Also, note that I can apparently mute the warning with an explicit cast. Note: I've set CS8619 to a severity of "error" in a .editorconfig file like this: |
@RikkiGibson At a first glance, this looks like #51886 ? I'm not quite sure. @Genbox, can you comment the code at lines from 55 to the end of the method, and see if this still reproduces? If it no longer produces, do you have a call somewhere in the commented out code that passes 'default' (either explicitly or as a default parameter value? e.g, |
Looks a lot like #51886. I can confirm that the issue disappears when I comment out the code. |
Hahaha, this is kinda funny. I've copied out the method and all classes it depends on, but still no dice. Can't reproduce it. |
Ah, I managed to create a reproduction. I've uploaded it here: https://github.com/Genbox/Repo-62500 Simply run "dotnet build" in the folder and it should give the error:
|
There is also #61516 |
Thanks for reporting this. |
I minimized your repro to this: #nullable enable
using System.Collections.Generic;
using System.Linq;
public class DataType
{
public string? BaseTypes { get; set; }
}
internal class DataTypeInfoManager
{
private async void GetTypesByIdsAsync()
{
List<DataType> dataTypes = new List<DataType>();
var dbTypes = dataTypes
.Select(s => new
{
s.BaseTypes,
});
foreach (var field in new[] { "" })
{
#nullable disable
string x = null; // comment this out. Warning disappear
_ = x;
#nullable enable
}
}
} |
Version Used:
Steps to Reproduce:
I am unable to create a small project that reproduces the compiler warning.
Instead, I'll give what I can so you can investigate the issue.
This is our data class:
The code that uses it, and have the CS8619 warning:
When we compile it, we get the following warning:
Note how the signatures mismatch on string/string? for BaseTypes.
I've put the code into sharplab.io and looked at the compiler output. It seems that anonymous types are not generated with nullable reference types, so
s.BaseTypes
above becomes a string, instead of string?. The compiler detects this string -> string? mismatch and report CS8619.The text was updated successfully, but these errors were encountered: