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

  • Home
  • SEARCH
  • 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 8049319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:45:28+00:00 2026-06-05T06:45:28+00:00

Consider the class: public class foo { public object newObject { get { return

  • 0

Consider the class:

public class foo
{
    public object newObject
    {
        get
        {
            return new object();
        }
    }
}

According to MSDN:

Properties are members that provide a flexible mechanism to read,
write, or compute the values of private fields. Properties can be used
as though they are public data members, but they are actually special
methods called accessors. This enables data to be accessed easily

And:

Properties enable a class to expose a public way of getting and
setting values, while hiding implementation or verification code.

A get property accessor is used to return the property value, and a
set accessor is used to assign a new value. These accessors can have
different access levels. For more information, see Accessor
Accessibility.

The value keyword is used to define the value being assigned by the
set indexer.

Properties that do not implement a set method are read only.
while still providing the safety and flexibility of methods.

Does this therefore mean that at some point in time the value of the newObject property has a reference to the returned new object?

edit removed readonly from property

edit2 also would like to clarify that this is not the best use for a property but its done to try and illustrate the question more effectively.

  • 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-05T06:45:29+00:00Added an answer on June 5, 2026 at 6:45 am

    You return a new object on each access to the property and that is not the expected behavior of properties. Instead you should return the same value each time (e.g. a value stored in a field). A property getter is simply glorified syntax for a method that returns a value. Your code compiles into something like this (the compiler creates a getter by prefixing the property name with get_ which is then emitted as IL):

    public class foo
    {
        public object get_newObject()
        {
            return new object();
        }
    }
    

    Each call to the getter will create a new object that foo doesn’t know about or has access to.

    Does this therefore mean that at some point in time the value of the newObject property has a reference to the returned new object?

    No.


    Property using a backing field:

    class Foo {
    
      readonly Object bar = new Object();
    
      public Object Bar { get { return this.bar; } }
    
    }
    

    Using automatic properties:

    class Foo {
    
      public Foo() {
        Bar = new Object();
      }
    
      public Object Bar { get; private set; }
    
    }
    

    A property is accessed using the same easy syntax as a public field. However, by using a property you can add code to the getter and the setter allowing you to do stuff like lazy loading in the getter or validation in the setter (and much more).

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

Sidebar

Related Questions

Consider the following class hierarchy: public class Foo { public string Name { get;
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
Consider: class Foo { public string Name{get;set;} public string[] WebSites{get;set;} } I have a
Consider the following code: public class Foo { private static final Object LOCK =
Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider the following Java class: public class Foo { public static void doStuff() {
Please consider the following simple use case: public class Foo { public virtual int
Consider the following program: class A { public static void Foo() { } }
Consider the following class public class Class1 { public int A { get; set;
Consider the following case: public class A { public A() { b = new

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.