readonly field not initialized - no warning

Viewed 93

I have the following class with an obviously uninitialized readonly field. This compiles fine with no erros or warnings:

public class Test
{
    readonly string s;

    public string GetString() => this.s.ToLower();
}

The fun part comes now. When I explicitly declare the field as private (which is default anyway, isn't it?) I now get a warning as I would expect:

public class Test
{
    private readonly string s;

    public string GetString() => this.s.ToLower();
}

warning CS0649: Field 'Test.s' is never assigned to, and will always have its default value null

Is this a Bug?

Iam using Microsoft Visual Studio Community 2019, Version 16.9.3.

0 Answers
Related