# STRUCTURE INSTANCE OR REFERENCE

Page 1 of 1

## 8 Replies - 3966 Views - Last Post: 11 November 2010 - 02:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=199559&amp;s=1f625c4757864a0f8d494c0b401bf403&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nczimm

Reputation: 0
• Posts: 7
• Joined: 14-December 09

# STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 09:23 AM

I have the following example from an MCTS training book. The statement from the example says
"This code would display “n1 = 1, n2 = 2” because a structure is a value type, and
assigning a value type results in two distinct values". If this is the case then "Numbers n2 = n1;"
creates an instance of numbers and not a reference to the instance n1. I thought the "new" keyword was required to create an instance of a structure. I could use some clarification.

```struct Numbers
{
public int val;
public Numbers(int _val)
{ val = _val; }
public override string ToString()
{ return val.ToString(); }
}

Numbers n1 = new Numbers(0);
Numbers n2 = n1;
n1.val += 1;
n2.val += 2;
Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);

```

This code would display “n1 = 1, n2 = 2” because a structure is a value type, and
assigning a value type results in two distinct values

Is This A Good Question/Topic? 0

## Replies To: STRUCTURE INSTANCE OR REFERENCE

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,235
• Joined: 14-September 07

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 09:57 AM

structs derive from System.ValueType. Thus, even when allocated with new() they are still on the stack rather then on the heap. Assignment gives you a shallow, memberwise copy of the elements (which is all you need since your struct is only holding other ValueTypes, in this case, System.Int32).

### #3 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 10:00 AM

Hello there,

In the code you provided, this line:

```Numbers n1 = new Numbers(0);
```

is creating a new object (a struct in this case), and assigning it to the stack. This is because all value types are assigned to the stack. This means that when you use the 'n1' variable, you are working directly with the struct object.

However, when you use the 'new' keyword with reference types (like classes), you are again creating a new object, however, this time the object is stored on the heap. If the 'Numbers' struct was a class, 'n1' would only hold a reference to that object that you just created on the heap. You would never be working directly with that object; you would only be working with a reference to it.

Not sure if this helps you...

Therefore, in this line (taking 'Numbers' to be a struct):

```Numbers n2 = n1;
```

'n2' is being assigned to 'n1'. Basically, 'n2' becomes an independent copy of 'n1'. Therefore, any changes made to 'val' in 'n2' will NOT be reflected in 'n1', as 'n2' is now an unrelated copy of 'n1'.

However, if we imagine that 'Numbers' was a class again, the above line would mean we were assigning a copy of the reference to the 'Numbers' object to 'n2'. Consequently, both 'n2' and 'n1' will point to the same point in memory (as they now hold the same reference); they will point to the SAME object (i.e. the 'Numbers' object). Therefore, any changes made to 'val' via 'n2' WILL be reflected in 'n1', as both point to the same object, so one change in one, if reflected in the other.

Don't know if this helps at all...

This post has been edited by [email protected]: 11 November 2010 - 10:21 AM

### #4 nczimm

Reputation: 0
• Posts: 7
• Joined: 14-December 09

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 11:54 AM

What does "Assignment gives you a shallow, memberwise copy of the elements " mean? In particular the meaning of a "shallow" copy of the element and a "memberwise" copy of the element. Can these copies be treated like an instance?

[email protected], on 11 November 2010 - 09:00 AM, said:

Hello there,

In the code you provided, this line:

```Numbers n1 = new Numbers(0);
```

is creating a new object (a struct in this case), and assigning it to the stack. This is because all value types are assigned to the stack. This means that when you use the 'n1' variable, you are working directly with the struct object.

However, when you use the 'new' keyword with reference types (like classes), you are again creating a new object, however, this time the object is stored on the heap. If the 'Numbers' struct was a class, 'n1' would only hold a reference to that object that you just created on the heap. You would never be working directly with that object; you would only be working with a reference to it.

Not sure if this helps you...

Therefore, in this line (taking 'Numbers' to be a struct):

```Numbers n2 = n1;
```

'n2' is being assigned to 'n1'. Basically, 'n2' becomes an independent copy of 'n1'. Therefore, any changes made to 'val' in 'n2' will NOT be reflected in 'n1', as 'n2' is now an unrelated copy of 'n1'.

However, if we imagine that 'Numbers' was a class again, the above line would mean we were assigning a copy of the reference to the 'Numbers' object to 'n2'. Consequently, both 'n2' and 'n1' will point to the same point in memory (as they now hold the same reference); they will point to the SAME object (i.e. the 'Numbers' object). Therefore, any changes made to 'val' via 'n2' WILL be reflected in 'n1', as both point to the same object, so one change in one, if reflected in the other.

Don't know if this helps at all...

### #5 nczimm

Reputation: 0
• Posts: 7
• Joined: 14-December 09

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 12:28 PM

After a little digging I found a shallow copy of a structure to be a reference to that structure. Is this correct? If so I am back to my original question.

Thanks again

This post has been edited by nczimm: 11 November 2010 - 12:28 PM

### #6 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 12:52 PM

In c#, the MemberwiseClone method creates a shallow copy of object. Below is the Microsoft take on a shallow copy and, in part, a deep copy:

Quote

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object, In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

However, I always thought, rather confusingly, that a MemberWise copy, in general, is a deep copy (despite the fact that the c# method above provides a shallow copy). Where as a shallow copy is referred to as a bitwise copy. Please correct me if I'm wrong someone.

In the scenario you have provided, I don't think it is correct to talk about references. 'n2' is assigned a copy of the struct held within 'n1'.

With references, the reference sits on the stack, and the object sits on the heap. The reference points to the object.

With structs, which are value types, the struct object is put directly on the stack, therefore there is no need for a reference. You interact directly with the object on the stack. When you assign 'n1' to 'n2', a shallow copy is made and put on the stack. Therefore, you have two independant copies of the object on the stack. No references are involved really. When using 'n1' or 'n2', you are basically dealing directly with the struct objects.

This post has been edited by [email protected]: 11 November 2010 - 01:28 PM

### #7 nczimm

Reputation: 0
• Posts: 7
• Joined: 14-December 09

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 02:18 PM

[email protected], on 11 November 2010 - 11:52 AM, said:

In c#, the MemberwiseClone method creates a shallow copy of object. Below is the Microsoft take on a shallow copy and, in part, a deep copy:

Quote

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object, In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

However, I always thought, rather confusingly, that a MemberWise copy, in general, is a deep copy (despite the fact that the c# method above provides a shallow copy). Where as a shallow copy is referred to as a bitwise copy. Please correct me if I'm wrong someone.

In the scenario you have provided, I don't think it is correct to talk about references. 'n2' is assigned a copy of the struct held within 'n1'.

With references, the reference sits on the stack, and the object sits on the heap. The reference points to the object.

With structs, which are value types, the struct object is put directly on the stack, therefore there is no need for a reference. You interact directly with the object on the stack. When you assign 'n1' to 'n2', a shallow copy is made and put on the stack. Therefore, you have two independant copies of the object on the stack. No references are involved really. When using 'n1' or 'n2', you are basically dealing directly with the struct objects.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 02:19 PM

Arrays are a reference type.

### #9 nczimm

Reputation: 0
• Posts: 7
• Joined: 14-December 09

## Re: STRUCTURE INSTANCE OR REFERENCE

Posted 11 November 2010 - 02:21 PM

I believe you have described the situation perfectly. As usual there is much more to the subject than I anticipated. Your help has been very valuable.

Thanks