none
c# Storing a reference to an mutable class object in a struct a bad idea? RRS feed

  • Question

  • I know that mutable structs are a terrible idea, but I've thought about having a struct hold a reference to an mutable class instead like this:

        class Program
        {
            static void Main(string[] args)
            {
                MutableStuff Stuff = new MutableStuff();
                Stuff.VAL = 1;
                Stuff.VAL2 = 2;
                Struct Struct = new Struct(Stuff);
                Console.ReadKey();
            }
        }
        public class MutableStuff
        {
            public int VAL { get; set; }
            public int VAL2 { get; set; }
        }
        public struct Struct
        {
            public MutableStuff Stuff { get; }
            public Struct(MutableStuff Stuff)
            {
                this.Stuff = Stuff;
            }
        }

    They way is see it the struct is immutable and only holds a reference so it shouldn't cause problems but is this true?




    • Edited by Varscott11 Sunday, February 4, 2018 9:45 PM Don't know why I put immutable... sorry
    Sunday, February 4, 2018 9:41 PM

Answers

  • Hello Varscott11,

    For a value type that contains refer type "field", the memory structure like below.

    NOTE. The structure "instance" exists on a stack and it contains a refer address(4 byte on 32bit OS). The refer instance exist on the heap.

    >>They way is see it the struct is immutable and only holds a reference so it shouldn't cause problems?

    As for your code, I'm not sure what you want to do with the structure, it looks no problem but meaningless. If you pass the structure as parameter this will add stack memory because it is value type, it will pass a structure and copy ones rather than pass a address directly. These two structures are independent individuals.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Monday, February 5, 2018 9:56 AM
    • Proposed as answer by Cor Ligthert Monday, February 5, 2018 9:59 AM
    • Marked as answer by Varscott11 Monday, February 5, 2018 1:27 PM
    Monday, February 5, 2018 8:37 AM
    Moderator

All replies

  • Hello Varscott11,

    For a value type that contains refer type "field", the memory structure like below.

    NOTE. The structure "instance" exists on a stack and it contains a refer address(4 byte on 32bit OS). The refer instance exist on the heap.

    >>They way is see it the struct is immutable and only holds a reference so it shouldn't cause problems?

    As for your code, I'm not sure what you want to do with the structure, it looks no problem but meaningless. If you pass the structure as parameter this will add stack memory because it is value type, it will pass a structure and copy ones rather than pass a address directly. These two structures are independent individuals.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Monday, February 5, 2018 9:56 AM
    • Proposed as answer by Cor Ligthert Monday, February 5, 2018 9:59 AM
    • Marked as answer by Varscott11 Monday, February 5, 2018 1:27 PM
    Monday, February 5, 2018 8:37 AM
    Moderator
  • Hello Cor,

    Thanks for your point, a nice day.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 5, 2018 9:51 AM
    Moderator
  • I plan on creating the structure once and making it accessible by many objects but it never gets passed around anywhere else. I appreciate you filling me in on some much more needed knowledge on structures. Cheers!
    Monday, February 5, 2018 1:36 PM