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

C#7 inline As variable scope is too wide #19129

Closed
pixellos opened this issue Apr 29, 2017 · 3 comments
Closed

C#7 inline As variable scope is too wide #19129

pixellos opened this issue Apr 29, 2017 · 3 comments
Labels
Area-Language Design Question Resolution-Answered The question has been answered Resolution-By Design The behavior reported in the issue matches the current design

Comments

@pixellos
Copy link

pixellos commented Apr 29, 2017

Versions Used:
2.1.0 (nuget from 24-04-2017),

Release version from
Internal ID: Roslyn-Signed_20170320.4
Internal VS ID: d15rel.26403.00

Steps to Reproduce:

  1. new file test.cs with
using System; 

namespace Test
{
    public class Program
    {
        public static void Main()
        {
            Exception e = null;
            if (e is ArgumentException ae)
            {
            }
            if  (e is ArgumentException ae)
            {
            }
        }
    }
}
  1. Compile with csc.exe from newest release
    https://github.com/dotnet/roslyn/releases/tag/Visual-Studio-2017-Update-1

Expected Behavior:
Build should have pass.
This construct should be treated like

Exception e = null;
{
    var ae = e as ArgumentException;
    if (ae != null)
    {
    }
}
{
    var ae = e as ArgumentException;
    if (ae != null)
    {
    }
}

Actual Behavior:
Microsoft (R) Visual C# Compiler version 2.1.0.56735
Copyright (C) Microsoft Corporation. All rights reserved.

test.cs(15,17): error CS0128: A local variable or function named 'ae' is already defined in thi
s scope

Code is treated like

Exception e = null;
var ae = e as ArgumentException;
if (ae != null)
{
}
var ae = e as ArgumentException;
if (ae != null)
{
}
@sharwell
Copy link
Member

sharwell commented Apr 30, 2017

The behavior you are observing is actually by design. It was implemented in order to support common scenarios like this:

if  (!(e is ArgumentException ae))
{
  return;
}

// do something with ae here

📝 Note that I wasn't part of the design for this feature. I'm simply telling you how the outcome of the language design team's work on this feature relates to the behavior you observed.

@sharwell
Copy link
Member

I'm leaving this open so one of the language design members can add more context and/or links to related issues to help you understand how to best use this new feature. 😄

@jcouv
Copy link
Member

jcouv commented Oct 22, 2017

This is indeed by design.
You can find more details in the pattern-matching proposal and issue #12597.

I'll go ahead and close this. Thanks

@jcouv jcouv closed this as completed Oct 22, 2017
@jcouv jcouv added Resolution-Answered The question has been answered Resolution-By Design The behavior reported in the issue matches the current design labels Oct 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Question Resolution-Answered The question has been answered Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

3 participants