3

Let's say we have a program that works like this:

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            Storage MainStorage = new Storage();

            PrintData Printer = new PrintData();

            Printer.GetStorage(MainStorage);
        }
    }

    class Storage
    {
        //Stores some data
    }

    class PrintData
    {
        Storage storage;

        public void GetStorage(Storage storage)
        {
            this.storage = storage;
        }

        public void Print()
        {
            //Prints data
        }
    }
}

It's just an example so the code wont be perfect but my question is in this instance does the GetStorage() method make a copy of the MainStorage object or does it make a reference to it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

4

Its just an example so the code wont be perfect but my question is in this instance does the GetStorage() method make a copy of the MainStorage object or does it make a reference to it?

It makes a copy of the reference to the Storage instance. In this case, the field in PrintData will reference the same instance in memory as the one you assign to MainStorage.

You can check to see whether assignment has reference semantics by checking whether the type is a class or a struct. If it's a value type (struct), then the copy will have value copying semantics.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • And what happens if i decide to modify the 'storage' object within 'PrintData' – Ognian Mirev Dec 30 '14 at 23:37
  • Mutating a reference-type object will be "seen" by every other object that refers to it. This doesn't happen to structs, as the specific copy is altered (correct me if I'm wrong, but doesn't it reallocate it, too?). A good example is that a website is a reference object - you change the front page and everyone sees it update. Meanwhile, a photocopied piece of paper is a value-type object - you can edit your copy, but mine stays the same. – Kyle Baran Dec 30 '14 at 23:44
  • Reed, would you say this question is a duplicate of http://stackoverflow.com/questions/1096449/c-sharp-string-reference-type? – John Saunders Dec 30 '14 at 23:48
  • @JohnSaunders Not really - it's related, but different – Reed Copsey Dec 31 '14 at 01:17
  • Seems there must be a canonical version of this question somewhere. – John Saunders Dec 31 '14 at 01:21
  • I'd add that not only structs are copied, but all value types (includes Enumerations, which are not structs technically)... and the best way to check for it would be checking `theObject.GetType().IsValueType` :-) – Jcl Dec 31 '14 at 08:36
1

In this example, it gets a copy of the reference, not the object itself.

Classes are always copied by reference. Structs and enums are copied by value unless you use the keyword ref in the method parameter.

String is a special case of class, being immutable (cannot be changed), whenever you modify it the reference points to a new object representing the result, so, it's a copy of the value effectively.

Meligy
  • 35,654
  • 11
  • 85
  • 109