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

Incorrect description of is var behavior #11804

Closed
Zenexer opened this issue Apr 12, 2019 — with docs.microsoft.com · 1 comment · Fixed by #11870
Closed

Incorrect description of is var behavior #11804

Zenexer opened this issue Apr 12, 2019 — with docs.microsoft.com · 1 comment · Fixed by #11870
Assignees
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc
Milestone

Comments

Copy link

Zenexer commented Apr 12, 2019

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#-var-pattern- states the following:

A pattern match with the var pattern always succeeds for non-null expressions; if expr is null, the is expression is false. The non-null value of expr is always assigned to a local variable the same type as the runtime time type of expr. Its syntax is:

expr is var varname

The following example uses the var pattern to assign an expression to a variable named obj. It then displays the value and the type of obj.

It then provides an example, of which the key component is the following:

object[] items = { new Book("The Tempest"), new Person("John") };
foreach (var item in items) {
if (item is var obj)
  Console.WriteLine($"Type: {obj.GetType().Name}, Value: {obj}"); 
}

This very clearly and unambiguously indicates that item is var obj will return false if item is null. This assertion is incorrect, and there are multiple references, including on https://github.com/dotnet/csharplang, stating that is var will always return true, regardless of nullability.

This error seems to be the cause of some confusion:

There was a proposal to add this behavior, but it was declined: dotnet/csharplang#792


Document Details

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

@Thraka Thraka added doc-bug Problem with the content; needs to be fixed [org][type][category] P1 and removed ⌚ Not Triaged Not triaged labels Apr 16, 2019
@Thraka
Copy link
Contributor

Thraka commented Apr 16, 2019

@BillWagner I just verified that is var does indeed always return true. I tested both c# 7.3 and 8.0.

@Thraka Thraka added this to the Backlog milestone Apr 16, 2019
@BillWagner BillWagner modified the milestones: Backlog, April 2019 Apr 16, 2019
BillWagner added a commit to BillWagner/docs that referenced this issue Apr 16, 2019
BillWagner added a commit that referenced this issue Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants