dotnet / dotnet/csharpstandard

declaration expression with var as type name

Open
#1,254 3 comments 0 reactions 1 assignee Claimed by @jskeet View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

**Describe the bug**

C# draft-v8 §12.17 (Declaration expressions) says:

> A declaration expression that is a simple discard or where the *local_variable_type* is the identifier `var` is classified as an implicitly typed variable.

That should not apply if name lookup for `var` finds a type.

**Example**

```csharp
using var = char;

class C {
static void M(out char x) {
x = 'U';
}

static void M(out int x) {
x = 42;
}

static void N()
{
M(out var x);
}
}
```

**Expected behavior**

In the call `M(out var x)`, the declaration expression `var x` should not be classified as an implicitly typed variable. The call should unambiguously resolve to `static void M(out char x)`.

(If `var x` were an implicitly typed variable as the current wording says, then the call would also match `static void M(out int x)` and the ambiguity would cause a compile-time error.)

**Additional context**

Found while looking for how the `var` contextual keyword is defined and whether the rules for `notnull` are similar ().

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.