Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.