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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:33:19+00:00 2026-05-29T11:33:19+00:00

A struct is a value type, so if I assign a struct to another

  • 0

A struct is a value type, so if I assign a struct to another struct, its fields will be copied in the second struct. But, what happens if some fields of the struct are a reference type?

public struct MyIPEndPoint
{
    public String IP;
    public UInt16 Port;

    public MyIPEndPoint(String ipAddress, UInt16 portNumber)
    {
        IP = ipAddress;
        Port = portNumber;
    }

    public override string ToString()
    {
        return IP+":"+Port;
    }
}

...

static int Main(string[] args)
{
    MyIPEndPoint address1 = new MyIPEndPoint("127.0.0.1", 8080);
    MyIPEndPoint address2 = address1;

    address2.IP = "255.255.255.255";
    address2.Port = 9090;

    Console.WriteLine(address1);
    Console.WriteLine(address2);
}

The output is:

127.0.0.1:8080
255.255.255.255:9090

Why the IP (a string, that is a reference type) of address1 does not change?
The same behavior occurs if I replace string with IPAddress to represent the IP within MyIPEndPoint: although IPAddress is a class (that is a reference type), it does not behave as a reference type. Why?

Indeed, if I wrap the string which represent the IP using a new simple class MyIP, the behavior changes.

public class MyIP
{
    public string IpAsString;

    public MyIP(string s)
    {
        IpAsString = s;
    }
    public override string ToString()
    {
        return IpAsString;
    }
}

Of course you should also adjust the MyIPEndPoint struct in the following way:

public struct MyIPEndPoint
{
    public MyIP IP;   // modification
    public UInt16 Port;

    public MyIPEndPoint(String ipAddress, UInt16 portNumber)
    {
        IP = new MyIP(ipAddress);   // modification
        Port = portNumber;
    }

    public override string ToString()
    {
        return IP+":"+Port;
    }
}

Finally in the Main I changed only a statement:

MyIPEndPoint address1 = new MyIPEndPoint("127.0.0.1", 8080);
MyIPEndPoint address2 = address1;

address2.IP.IpAsString = "255.255.255.255";   // modification
address2.Port = 9090;

Console.WriteLine(address1);
Console.WriteLine(address2);

Now the output is:

255.255.255.255:8080
255.255.255.255:9090

I was expecting this output in the first case.
Why in the first case does the reference not behave as expected?

  • 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-05-29T11:33:19+00:00Added an answer on May 29, 2026 at 11:33 am

    You have correctly understood that with structs, address1 and address2 are not the same object. The values were copied. However, for the field, this is a simple case of reassignment. It has nothing to do with the fact that string is a reference type or any special rules or any suggestion of immutability. You have simply reassigned a property or field with another value.

    someStruct.SomeString = "A";
    anotherStruct = someStruct;
    anotherStruct.SomeString = "B"; // would never affect someStruct
    

    You have overwritten the reference in this example. The fact that for a brief moment, both structs’ fields contained the same reference is of no importance. In your second example, you did something very different.

    someStruct.IP.SomeString = "A";
    anotherStruct = someStruct;
    anotherStruct.IP.SomeString = "B"; 
    

    In this case, the value of IP has not changed. Part of IP’s state has changed. Each struct’s field is still referencing the same IP.

    Put in simpler terms

    var foo = new Foo(); // Foo is class
    var other = foo; 
    // other and foo contain same value, a reference to an object of type Foo
    other = new Foo(); // was foo modified? no! 
    
    int x = 1;
    int y = x;
    y = 2; // was x modified? of course not.
    
    string s = "S";
    string t = s;
    t = "T"; // is s "T"? (again, no)
    

    Variables and fields hold values. For classes, those values are references to objects. Two variables or fields can hold the same reference, but that does not mean those variables themselves are linked. They are not connected in anyway, they simply hold a common value. When you replace a value for one variable or field, the other variable is not affected.


    Not on the specific topic, but it is worth noting that mutable structs are viewed by many as evil. Others don’t quite hold the same view, or at least not as religiously. (However, it is also worth noting that had Address been a class, then address1 and address2 would hold the same value (the reference to the Address object), and modification to the state of address1 would be visible via address2 as long as neither address1 or address2 are themselves reassigned.)

    If this is an actual representation of your code, it would be worth doing some research on mutable structs so you at least have a full understanding of various pitfalls you may encounter.

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

Sidebar

Related Questions

struct is a value type in C#. when we assign a struct to another
can we assign null value to struct type of variable? struct MyStruct { }
I have a simple value type: [Serializable] private struct TimerInstance { public TimerInstance(string str,
I think the GC may treat reference type and value type differently. GC will
I understand that struct is value type and class is reference type in .Net.
Class is a reference types while Struct is a value type. This means that
I have a situation where I have a simple, immutable value type: public struct
In .NET, a value type (C# struct ) can't have a constructor with no
I have some function to find a value: struct FindPredicate { FindPredicate(const SomeType& t)
Why do I hear all the time that int is a reference type, but

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.