Search code examples
c#monogamestatic-variables

Will assigning a static object to another (non-static) variable make a copy of that object?


I have a static class that holds static textures for use throughout the game.

public static class AssetManager
{

    public static Texture2D someTexture;
    ....
}

My question is, if I were to assign that static texture to a variable in another class, like this:

Texture2D classTexture = AssetManager.someTexture;

would I be creating a copy of that texture? I'm really not thinking I would be and reading about it, I don't think thats how a static variable should work. I tried it out and checked the hashCode of each and they seemed to be the same. But I'm also not sure if thats the correct way to be checking and I don't want to run into a problem later on where I'm creating a bunch of new textures that I don't need.


Solution

  • C# never makes implicit copies of objects.

    It kind of sounds like your caution may be coming from a C++ background, in which case you can think of all objects in C# as being a pointer.