views:

128

answers:

4

At assigning one field to another, does the C# just copy data over, or actually creates link? In this article there's an example of game engine structure. The coder there has components contain their parent. In C#, do they just contain the parent copy, or they refer to it?

Example code:

class World
{
    ...
    public void Update()
    {
        ...
        ent.OnAttach(this);
        ...
    }
    ...
}

class Entity
{
    ...
    public void OnAttach(World world)
    {
        world_ = world;
    }
    ...
}

Could the Entity object now access World object and have access to it's fields and methods, like in the artice? (or I misunderstood the code?)

+4  A: 

It depends on the World type. If it's a reference type (a class), the copied reference will point to the same object and thus changing the object pointed by the new reference will affect the original object (no new object is created). This is the case in the posted sample. The field will simply refer to the same World object.

If the World type is a value type, it's copied (along with its contents) and will become an entirely distinct value, changing which will not affect the original.

Mehrdad Afshari
I got too late to read this before the 2nd answer, second one seems more clearly, so sorry, but you explained the stuff great, thank you! +1
Johnny
+6  A: 

Because your data type World is defined as a class and not a struct that means that when you assign a variable of that type, only a reference to the same data is copied.

In other wrods, whether you then use world.SomeProperty = something or world_.someProperty = something they will be editing the same object in memory.

If you change your data type to be a struct then the entire data structure will be copied and you will have two copies of the same data.

Regardless of how you defined your data, once you have a reference to the data you can then access its methods or properties. So, yes, once your Entity object has a reference to the world object it can access any methods or properties on it (as long as they are not private).

Eilon
Loud and clear, nicely explained I got it on first slight look! +1, accepted answer!
Johnny
Another thing, is reference type still working the same if it was assigned to List<T>?In other words, would change of Entity object affect it's part in List<Entity> after adding it with List<Entity>.Add()?
Johnny
Nope, it would still be exactly the same object instance. The object doesn't even "know" that it was added to a list.
Eilon
+3  A: 

As Entity & World are class (i.e. "reference types"), then they would just have references to each other. If one was a struct (i.e. a "value type"), then the enite thing would be copied.

James Curran
+2  A: 

You should have a look at passing by reference and passing by value. Your World object is a passed by reference, so you're passing a reference to a location in memory where the data this object contains resides.

Entity could now only access World object's public methods, properties.

Tony
Uh, well if I'd assign (pass) it any possible way I'd pass it as reference, because it's reference type, am I wrong?
Johnny
you are correct, you can only pass this object by reference.
Tony
Ok, I just got kinda confused, because you mentioned passing by reference (connected with function arguments, 'ref' keyword), and not reference types. Anyways thanks for help.
Johnny
when you add the ref keyword to your argument, as that is what I'm talking about when saying 'pass by reference' you are allowing the reference to be changed, so your variable can reference something else with the ref keyword, where without WHAT it references cannot change, although the content of the object can change
Tony