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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:42:26+00:00 2026-06-01T23:42:26+00:00

As I understand it, properties can not return references, and since structs are value

  • 0

As I understand it, properties can not return references, and since structs are value types, there’s no way to return a reference to a struct via properties, which would enable:

public struct SomeStruct
{
   public int SomeMember { get; set; }
}
class foo
{
   private SomeStruct bar; 
   public SomeStruct Bar{ get { return bar; } set { bar = value; } }
}

//Somewhere else
foo f = new foo();
f.Bar.SomeMember = 42; //Error, this doesn't work

Will I have to resort to setMemberOfSomeStruct() or is there another way?

edit: Specifically, I want to avoid having to call new for structs like these all the time. I know that with an constructor SomeStruct(int), this would work:

f.Bar = new SomeStruct(42); //ugh
  • 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-01T23:42:28+00:00Added an answer on June 1, 2026 at 11:42 pm

    Make your struct implementations immutable and do this:

    var newSomeStruct = new SomeStruct(42 /* Feeds SomeMember */);
    var myFoo = new Foo();
    myFoo.Bar = newSomeStruct;
    
    public struct SomeStruct
    {
        private int _someMember;
    
        public int SomeMember { get { return _someMember; } }
    
        public SomeStruct(int someMember)
        {
            _someMember = someMember;
        }
    }
    

    Immutable structs help preserve the value type semantics that are expected.

    If you don’t make it immutable, then the above code is still the only way to get this to work.

    Alternatively, but not much nicer looking, expose a method on the class to set the class’s copy of SomeStruct:

    public void SetSomeMember(int val)
    {
        _bar.SomeMember = val; // Note _bar in this example is a field, not a property.
    }
    

    I only offer this for completeness, I’d still go down the immutability route as there is consensus regarding “mutable structs are evil”. Property getters is one gotcha, as is casting to interfaces – immutable structs solve these.

    There is also another facetious point to make – don’t assume that performance problems exist before performance problems actually exist. A class definition might be just as fast as a struct for the style of use you get from it. Profiling is king here.

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

Sidebar

Related Questions

I have trouble to understand how dependency properties can be used between C# and
I believe I understand properties for the most part. My question is, if I
As I understand, width/padding/margin properties only work on block level elements. However, on INPUT
I want to make sure I understand a key concept of properties and instance
I understood that anonymous types are marked private by the compiler and the properties
I understand the value of the three-part service/host/client model offered by WCF. But is
I know how to use properties and I understand that they implicitly call underlying
I understand the advantages of PROPERTIES over FIELDS, but I feel as though using
Someone please help me understand why this binding does not work... I have a
Variables, methods and classes can receive various security levels. From my C# experience, there

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.