Docs: Surely this is not the latest syntax specification?

Created on 11 Oct 2018  Â·  5Comments  Â·  Source: dotnet/docs

What about being able to test for null and declare a strong type in the same statement?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide P2 up-for-grabs

Most helpful comment

Hi, Petr,

I think I was getting confused with the is keyword. I was trying to find the new syntax for declaring a variable at the same time as testing it, e.g.:

if( sender is ComboBox myComboBox )
{
var item = myComboBox.SelectedItem;
}

My apologies.

*
Patrick


From: Petr Kulikov notifications@github.com
Sent: 11 October 2018 16:42
To: dotnet/docs
Cc: PatrickRyder; Mention
Subject: Re: [dotnet/docs] Surely this is not the latest syntax specification? (#8267)

@BillWagnerhttps://github.com/BillWagner I'm confused. The following still produces compile error:

object a = 4;
var b = a as int;

@PatrickRyderhttps://github.com/PatrickRyder did you mean the is expression, which is documented?
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#pattern-matching-with-is
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#is-expression

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/dotnet/docs/issues/8267#issuecomment-429004775, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AM5lM2yBkfvwmanAzfFc8WsToRyB1DCNks5uj2cDgaJpZM4XXrqX.

All 5 comments

Thanks for pointing this out @PatrickRyder

I thought I had updated this page as part of the additions for pattern matching in C# 7. I clearly missed it.

What needs to be added:

  • [ ] Using as with a strongly typed variable initializer.
  • [ ] Using as with value types
  • [ ] using as to perform null checking.~~

@BillWagner I'm confused. The following still produces compile error:

object a = 4;
var b = a as int;

@PatrickRyder did you mean the is expression, which is documented?
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#pattern-matching-with-is
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#is-expression

Hi, Petr,

I think I was getting confused with the is keyword. I was trying to find the new syntax for declaring a variable at the same time as testing it, e.g.:

if( sender is ComboBox myComboBox )
{
var item = myComboBox.SelectedItem;
}

My apologies.

*
Patrick


From: Petr Kulikov notifications@github.com
Sent: 11 October 2018 16:42
To: dotnet/docs
Cc: PatrickRyder; Mention
Subject: Re: [dotnet/docs] Surely this is not the latest syntax specification? (#8267)

@BillWagnerhttps://github.com/BillWagner I'm confused. The following still produces compile error:

object a = 4;
var b = a as int;

@PatrickRyderhttps://github.com/PatrickRyder did you mean the is expression, which is documented?
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#pattern-matching-with-is
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#is-expression

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/dotnet/docs/issues/8267#issuecomment-429004775, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AM5lM2yBkfvwmanAzfFc8WsToRyB1DCNks5uj2cDgaJpZM4XXrqX.

@pkulikov sigh you're right. That's what I get for looking at something when my brain was totally focused on a different problem.

I hid my previous comment as resolved. (That seemed the best strategy).

One change that would be useful on the page for as: Add a new sentence at the beginning of the Remarks section something like:

The is expression can both test that a conversion will succeed and conditionally assign a variable when the conversion will succeed. In many scenarios, it is more concise than using the as operator.

The is above would be linked to the keyword for `is.

@PatrickRyder thanks for raising the issue. I don't think you are the only one who confuses as and is, especially given, how close those two in functionality and usage. As far as I remember, ReSharper suggests refactoring that converts as usage to the pattern matching with is. So, the addition suggested by @BillWagner is "in-field" and it's good it gets to the docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mekomlusa picture mekomlusa  Â·  3Comments

svick picture svick  Â·  3Comments

ike86 picture ike86  Â·  3Comments

ite-klass picture ite-klass  Â·  3Comments

LJ9999 picture LJ9999  Â·  3Comments