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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:40:16+00:00 2026-06-17T15:40:16+00:00

I have an structure and a class public class MyClass { public string name;

  • 0

I have an structure and a class

public class MyClass
{
    public string name;
}

public struct MyStructure
{
    public MyClass classValue;
    public int intValue;
    public MyStructure(MyClass classValue, int intValue)
    {
        this.classValue = classValue;
        this.intValue = intValue;
    }
}

Elsewhere I write the following code:

MyClass class1 = new MyClass() { name = "AA" };
MyStructure struct1 = new MyStructure(class1, 4);
MyStructure struct2 = struct1;
struct1.classValue.name = "BB";  //struct2.classValue.name also change!

How can I make sure that every reference type member of a structure is copied by value in cases such as this?

  • 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-17T15:40:18+00:00Added an answer on June 17, 2026 at 3:40 pm

    Your MyStructure consists of an object reference of type MyClass and an int (Int32). The assignment

    MyStructure struct2 = struct1;
    

    copies just those data, the reference and the integer. No new instance of MyClass is created. You can’t change that.

    If you want to copy, you can say

    MyStructure struct2 = new MyStructure(
        new MyClass { name = struct1.classValue, },
        struct1.intValue
        );
    

    Of course, if you need to do this a lot, you can write a Clone() method to do just this. But you will have to remember to use the Clone method and not just assign.

    (By the way, the MyStructure is a “mutable struct”. Many people discourage them. If you mark the fields of MyStructure as readonly, I think “many people” will be satisfied.)

    Addition: See also Answer to Can structs contain references to reference types in C#.

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

Sidebar

Related Questions

Say I have a structure like: class SomeObject Public Name as String Public Created
I have this structure of classes: public class L3Message { public int Number {
For example I have a file name call A.java. Here its structure: public class
I have my class structure as follows public class Email { public string Subject
I have a directory structure like this: ./ +-- myClass | +-- MainClass.class |
My class: public class Device { int ID; string Name; List<Function> Functions; } And
So I have this class public static class MyClass { static MyClass() { ...
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
I have the following class structure public class Outer{ private Mapper a; .... private

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.