Skip to content

"Unassigned local variable" is reported for a variable, which is always assigned #80172

@DoctorKrolic

Description

@DoctorKrolic

Version Used:
VS 17.14.13

Steps to Reproduce:

int val;

try
{
	val = 1;
}
catch
{
	val = 2;
}
finally
{
    Console.WriteLine(val); // Use of unassigned local variable 'val'
}

Diagnostic Id:
CS0165

Expected Behavior:
No error since val is assigned in both try and catch "branches", so it is 100% assigned at the point of finally

Actual Behavior:
Compiler error CS0165: Use of unassigned local variable 'val'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions