-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
In most cases where var is allowed for inferring a type, the resulting inferred type is syntactically interchangeable. One exception is the following:
public void Method(object input)
{
if (input is object value)
{
// value has the static type 'object'
// this block executes only if input is non-null
}
if (input is var value)
{
// value has the static type 'object'
// this block executes even if input is null
}
}This behavior has several problems:
-
Aside from the obvious
is null,is varis the only cases where anisoperator can produce a true result when the left hand side is null. This is confusing. -
The pattern-matching
isoperator has the potential to support null checks on exposed values in concurrent code (Discussion on non-null reference types #788 (comment)), but usage in this case would require the type name always be explicitly specified. -
If this is not changed, then after the language supports nullable reference types we will have a case where
varcannot be replaced with the inferred type without a syntax error:string? local = ...; if (local is string a) { } if (local is var b) { } // b has the type 'string?' if (local is string? b) { } // syntax error, presumably equivalent to CS8116