CS8619 is reported on anonymous types
See original GitHub issueVersion Used:
- .NET 6.0.301
- Visual C# Compiler version 4.2.0-4.22281.5 (8d3180e5)
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:
public class DataType
{
public string ModuleId { get; set; }
public Module Module { get; set; }
public string Name { get; set; }
public string TypeName { get; set; }
public string DisplayName { get; set; }
public string? Description { get; set; }
public string? BaseTypes { get; set; }
}
The code that uses it, and have the CS8619 warning:
var dbTypes = await _db.DataTypes
.Where(s => typeIds.Contains(s.Id))
.Select(s => new
{
s.Id,
s.BaseTypes, //<-- here is the type that generates the warning
ModuleName = s.Module.Name,
})
.ToListAsync(token)
.ConfigureAwait(false);
When we compile it, we get the following warning:
DataTypeInfoManager.cs(43, 26): [CS8619] Nullability of reference types in value of type '<anonymous type: string Id, string Name, string TypeName, string DisplayName, string BaseTypes, string ModuleName, ICollection<DataField> DataFields>' doesn't match target type '<anonymous type: string Id, string Name, string TypeName, string DisplayName, string? BaseTypes, string ModuleName, ICollection<DataField> DataFields>'.
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.
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
C# anonymous type warning when Nullable Reference ...
CS8619 :Nullability of reference type in value of type <anonymous type: int ContractId, string Name, string Street> doesn't match type ...
Read more >Understanding Anonymous Types in C# -Don't use ... - YouTube
In this video, we understand Anonymous Types in C# and how we can use it in the context of automation testing, especially for...
Read more >Anonymous Types In C#
We can create anonymous types by using “new” keyword together with the object initializer. As you can see from the below code sample,...
Read more >Nullability of reference types doesn't match target type
And I'm getting this error: Warning CS8619 Nullability of reference types in value of type 'List<string?>' doesn't match target type ' ...
Read more >Understanding C# Anonymous Types
Anonymous objects in LINQ query. Passing an anonymous object as method parameter. Declaring and using an anonymous type object. The keyword var ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I minimized your repro to this:
Thanks for reporting this. Closing as duplicate of https://github.com/dotnet/roslyn/issues/51886