CSharp - Will the code compile: readonly field and constructor parameter

Question

Will the code compile

class ReadOnlyEx
{
    public readonly int nonStaticReadOnlyValue;
    public ReadOnlyEx(int x)
    {
        nonStaticReadOnlyValue = x;
    }
    //Some other code e.g.Main method() etc..
}


Click to view the answer

Yes.

Note

We can assign a value to a readonly field during the declaration or through a constructor.