Dofactory.com
Dofactory.com

What is the best way to assign a C# auto-property an initial value.

For C# 6 and higher use they autoproperty initializer syntax.


class Person
{
    // Assign initial value

    public string Name { get; set; } = "Johannes de Clerck";
}

For C# 5 and below use the constructor to initialize the default value.


class Person
{
    public Person()
    {
        // Assign initial value

        Name = "Johannes de Clerck";
    }
    public string Name { get; set; }
}


Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.