Skip to content
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

[Question] Why the output is CS0165 when using a is Constant pattern. #45582

Closed
InCerryGit opened this issue Jul 1, 2020 · 3 comments
Closed
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@InCerryGit
Copy link

Steps to Reproduce:

https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UkALIgLIAUAlJtrjQG4CGATogC4CmAzi4gLyIB2bAO6IAKhQDc1GjgCWAM1LsuiWRzGIAxgHt+tNo1GduPPvPoAbDm3LSZNEAHYpMezhAQAnKR16DRrgA6ADFtbXIXGgBfWBi4fDAxLFdcBGQIAAZEUO1MAHM2FglrIriooA==

Given the following C# example:

using System;
public class C {
    public void M() {
        var test = new T();
        if(test is T converTest == false)
            return;
        Console.WriteLine(converTest.Foo);
    }
}
public class T{
    public string Foo {get;set;}
}

The following result is returned:

CS0165: Use of unassigned local variable 'converTest '

if converTest is null, the method return . why the output is CS0165 when using a is Constant pattern?

@RikkiGibson
Copy link
Contributor

This would require a change to the definite assignment specification. If you want to see this happen, feel free to search for or open an issue in the csharplang repo.

@gafter
Copy link
Member

gafter commented Jul 1, 2020

Previously (incorrectly) reported at

The fact that people keep asking this question is perhaps evidence that something should be done. But the suggestion to do something belongs in csharplang. As it stands the compiler is obeying the specification.

@RikkiGibson
Copy link
Contributor

A spec change proposal has been submitted for discussion at dotnet/csharplang#4240.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

3 participants