Does "using static [class name]" turn its non-static members into static ones?


I have a MonoBehaviour that does the time-related stuff in my game.

public class WorldTime : MonoBehaviour

It has a static Method that I call when I want to update the time.

public static void AdvanceTime(int minutes)

It also has a nested TimeObject class that is meant as a general container to store any date that needs to be stored.

public class TimeObject
        public int Year {get; set; }
        public int Month {get; set; }
        public int Day {get; set; }
        public int Hour {get; set; }
        public int Minute {get; set; }
        public string Season { get; set; }
        public string MonthName {get; set; }
        public string DayName {get; set; }
        public int DayOfYear { get; set; }
        public int DayOfSeason { get; set; }
        public int MinutesAdded { get; set; }

In another script, I want to create an instance of TimeObject. I would like to make it easier and use TimeObject without having to write WorldTime.TimeObject, so I tried adding it in a ‘using’ directive, but could only do ‘using static’ as it’s not a namespace.

using static WorldTime;

void UpdateWeather(TimeObject time)

So now my worry is that I don’t know if the ‘using static’ directive would make TimeObject static or not.

I tried searching on Google but couldn’t find any result that I understood well enough to be sure that it’s not the case. (I’m low-intermediate level so lots of stuff is still blurry or unclear to me.)

Basically, I don’t want to create a TimeObject and have it be overridden by another one when I think I’m creating instances of it.

No, it does not change anything. using directives at the top of the file only change the type / member resolving inside this file. The types themselfs are not / can not be changed or altered.

Maybe start by reading the documentation about the using directive, more specifically the section about the static modifier.

All using static does is that the static members as well as nested types are directly available inside this file without the need to specify the type every time. So if you have a class like this:

public class MyClass
    public static readonly int MyConstant = 42;
    public int foo;
    public class MyNestedType
        public string name;

When you’re in another file, in order to access the static “MyConstant” you usually have to do

int val = MyClass.MyConstant;

When you put a using static MyClass; at the top, you can just do this:

int val = MyConstant;

The same holds true for nested types like “MyNestedType” in this example. Usually you have to do

MyClass.MyNestedType obj = new MyClass.MyNestedType();

with the using static you can do

MyNestedType obj = new MyNestedType();

It’s not recommended to use the using static very often as it makes the code much harder to read and you can easily run into ambiguity. If a file uses such a static import and later you add a static member to that imported class that also exists inside the file that imports the class, you would run into issues. Imagine this

using static MyClass;

public class MyComponent : MonoBehaviour
    public int MyConstant = 55;
    void Start()
        Debug.Log("MyConstant: " + MyConstant);

Here the member of our "MyComponent " class would be used instead. You should avoid importing large classes with many static members as the more things you import, the higher the chance for name collisions.

There are some rare exceptions where it might make sense to do that, For example when you have a file that implements a very complex math algorithm and you need to use tons of Mathf functions, one may use a using static so the fotmulas gets less noisy. However I would highly suggest to never use more than one such statement.