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 9102943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:30:27+00:00 2026-06-17T01:30:27+00:00

Observe the following… //pattern 1 public class Cheesesteak { public string bread {get; private

  • 0

Observe the following…

//pattern 1
public class Cheesesteak
{
    public string bread {get; private set}
    public string cheese {get; private set}

    public Cheesesteak()
    {
        bread = "Amoroso"; 
        cheese = "Cheez Whiz";
    }
}

//pattern 2
public class Cheesesteak
{
    public string bread 
    {
        get {return bread;}
        set 
        {
            bread = "Amoroso";
        }
    }
    public string cheese 
    {
        get {return cheese;}
        set
        {
            cheese = "Cheez Whiz";
        }
    }
    public Cheesesteak() {}
}

This is a curiosity question. Is there any advantage or particular reason that you would set the variables in the definition of the “set” versus declaring them in the constructor? My initial guess is pattern 1 is shorter, but less efficient during compile.

  • 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-06-17T01:30:28+00:00Added an answer on June 17, 2026 at 1:30 am

    Is there any advantage or particular reason that you would set the variables in the definition of the “set” versus declaring them in the constructor?

    No, and in fact, this is probably not what you want at all. This will make it impossible to set “break” or “cheese”, as any call, such as bread = "rye";, would set it to “Amoroso” (if it worked, but will cause a StackOverflowException). Also note that trying to retrieve the value in your code will cause a StackOverflowException, and the property getter returns the property and not a backing field value.

    You were likely thinking of this:

    public class Cheesesteak
    {
        private string bread = "Amoroso";
        public string Bread 
        {
            get {return bread;}
            set 
            {
                bread = value;
            }
        }
    
        // ...
    

    The only advantage here is you’re setting the “default” value where the field is defined, which can help with maintainability or readability in some cases, and even potentially eliminate the need for a defined constructor, which might reduce the overall length of code.

    My initial guess is pattern 1 is shorter, but less efficient during compile.

    In general, setting the fields inline vs. setting them in a constructor does not make this less efficient. The compiler will cause the type’s actual constructor to set the fields first then run the constructor code, so both versions end up (for practical purposes) the same in terms of the compiled IL. This isn’t a matter of efficiency, but rather of code readability and maintainability.

    Note that, if you wanted the property to always be a constant (ie: Bread should always return "Amoroso"), you can just make the property have a getter and no setter:

    public string Bread { get { return "Amoroso"; } }
    

    I suspect this is not the case, but I thought I’d mention it as an option just in case it’s what you intended.

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

Sidebar

Related Questions

Observe the following .NET type: public class X { public DateTime Timestamp {get;set;} public
Observe the following code: class A { public: virtual void Foo() = 0; int
Why does this happen? Please observe the following code: static class StringExtension { public
Observe the following: #include <iostream> #include <string> #include <cstdlib> int main(){ static std::string foo
Currently, I observe in hbernate3, the following behavior. if I have @BatchSize(size=5) set, then
Observe the following code: private List<List<Place>> m_grid = constructGrid(10000); private static List<List<Place>> constructGrid(int size)
Observe the following program written in Java (complete runnable version follows, but the important
Observe the following Python script, Script.py: import subprocess src_directory = 'Z:\z_7z\Some_Directory' zip_file_name = 'Test.7z'
I observe the following strange behaviour in Vim 7.3: When I'm below a fold,
With a third party API I observed the following. Instead of using, public static

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.