How to give a default value to a C# Auto-Property? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to give a default value to a C# Auto-Property?

NickFNickF USAPosts: 132Member

Can someone please let me know what is the best way to set a default value to a C# Auto-Property? Is it [possible using a constructor?

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member

    Yes, you can use the constructor to do it.

    class Test
    {
        public Test()
        {
            //do anything before variable assignment
    
            //assign initial values
            Name = "Default Name";
    
            //do anything after variable assignment
        }
        public string Name { get; set; }
    }
    
Sign In or Register to comment.