Skip to content

Local variable initialization is not always equivalent to immediate assignment after declaration #899

@jskeet

Description

@jskeet

From #837, in statements.md:

A local variable declaration that declares multiple variables is equivalent to multiple declarations of single variables with the same type and ref_kind. Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.

@KalleOlaviNiemitalo wrote:

It no longer corresponds exactly, because of ref safety.

using System;
public class C {
    public void M() {
        Span<int> s1 = stackalloc int[1]; // OK
        Span<int> s2;
        s2 = stackalloc int[1]; // error CS8353
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions