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.

CS8604 False-negative in .Net Standard project vs .Net Core

See original GitHub issue

Describe the bug

In a .Net Standard project, trying to use a Stream? instead of a Stream raises a CS8604 warning. But not when using a Stream? to initialize a StreamReader. In a .Net Standard project, both situations raise a CS8604 warning.

Steps To Reproduce

Here is the short class that I am using, with <Nullable>enable</Nullable> in .NetStandard 2.1 project and .NetCoreApp 3.1 project.

public class Test
{
    public async Task<Stream?> GetFile(string file)
    {
        Stream? stream = null;

        if (File.Exists(file))
        {
            stream = File.OpenRead(file);
        }

        return await Task.FromResult(stream);
    }

    public async Task TestStreamReader(string file)
    {
        using(Stream stream = await GetFile(file))
        {
            // Do stuff...
        }

        using(StreamReader reader = new StreamReader(await GetFile(file)))
        {
            // Do stuff...
        }
    }
}

Expected behavior

CS8604 for both using Stream and StreamReader in .Net Standard and .Net Core

Actual behavior

CS8604 for Stream in .Net Standard and for Stream and StreamReader in .Net Core

Analyzer

Package: Microsoft.CodeAnalysis.FxCopAnalyzers

Version: v3.3.0 (Latest)

Diagnostic ID: CA1716

Additional context

I understand this might be “intentional” as it should only work properly in .Net Core App if I understand this correctly but I feel like the fact that this is “half” working in .Net Standard can make it confusing.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
svickcommented, Aug 13, 2020

Am I correct to assume that with net5.0 on its way those annotations will not find their way to netstandard2.1 ?

Yes, but there is also a project that lets you copy annotations from one target framework to another.

1reaction
RikkiGibsoncommented, Aug 12, 2020

Correct, there are no plans that I am aware of to introduce nullability annotations to netstandard2.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deal with in Net 6's nullable and Entity Framework entities
If I know that my property is non-nullable and when migration generate it should have nullable false then I put this way. public...
Read more >
Demystifying .NET Core and .NET Standard
NET family, there's much confusion about .NET Core and .NET Standard and how they differ from the .NET Framework. In this article, I'll...
Read more >
How to use Nullable Reference Types in .NET Standard ...
In the previous post, I've explained how to use Nullable Reference Types and why this is a good feature. However, this feature works...
Read more >
C# 11 and . NET 7 - Modern Cross-Platform Development ...
Adding a second project using Visual Studio Code • 30 ... NET Standard; creating the simplest application possible with C# 11 and ....
Read more >
Migrating from .NET Framework to .NET Core: Tips and Tricks
When you migrate a project to .NET Standard/.NET Core you'll have to adopt the “Microsoft.NET.Sdk” csproj format. The process of changing ...
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