Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:53:38+00:00 2026-05-15T07:53:38+00:00

Is it possible to set a default value without the body of a property?

  • 0

Is it possible to set a default value without the body of a property? Preferably with annotations.

[SetTheDefaultValueTo(true)]
public bool IsTrue { get; set; }

[SetTheDefaultValueTo(false)]
public bool IsFalse { get; set; }

public void Something()
{
    var isTrue = this.IsTrue;
    var isFalse = this.IsFalse;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:53:38+00:00Added an answer on May 15, 2026 at 7:53 am

    No, there is no built-in way to set the value of a property with metadata. You could use a factory of some sort that would build instances of a class with reflection and then that could set the default values. But in short, you need to use the constructors (or field setters, which are lifted to the constructor) to set the default values.

    If you have several overloads for your constructor, you may want to look at constructor chaining.

    Using C# 6+, you are able to do something like this…

    public string MyValue { get; set; } = "My Default";
    

    Oh, it gets more fun because people have even requested something like this…

    // this code won't compile!
    public string MyValue {
        private string _myValue;
        get { return _myValue ?? "My Default"; }
        set { _myValue = value; }
    }
    

    … the advantage being that you could control the scope of the field to only be accesible in the property code so you don’t have to worry about anything else in your class playing with the state without using the getter/setter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write similar construction? I want to set, somehow, default value
How can I setup a default value to a property defined as follow: public
Is it possible to set a default value for columns in JPA, and if,
I'm trying to set the default value of a check box to be true
In Django, is it possible to set the default value of a form field
Is it possible to set a default value to some of arguments in Racket?
Possible Duplicate: default value for a static property I am able to assign default
Is it possible to set a default value in a 'file browse' option in
Is it possible to set the DataFormatString property of a column or cell in
Is it possible to have final transient fields that are set to any non-default

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.