What about being able to test for null and declare a strong type in the same statement?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
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:
as with a strongly typed variable initializer.as with value typesas 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
isexpression 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 theasoperator.
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.
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.