question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CS8619 is reported on anonymous types

See original GitHub issue

Version 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:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Youssef1313commented, Jul 8, 2022

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:

DataTypeInfoManager.cs(16,36): error CS8619: Nullability of reference types in value of type '<anonymous type: string 
BaseTypes, ICollection<DataField> DataFields>' doesn't match target type '<anonymous type: string? BaseTypes, 
ICollection<DataField> DataFields>'

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
        }
    }
}
0reactions
jcouvcommented, Jul 8, 2022

Thanks for reporting this. Closing as duplicate of https://github.com/dotnet/roslyn/issues/51886

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found