Tempest
How to tell if a variable is a reference or value data type
Today I've been learning about the ref, in, and out keywords. I understand that some variables are value types and some are reference types but I haven't really found and easy way of being able to tell which is which.
I understand that if I use the class keyword it will be a reference type and if I use struct it will be value. Is it really as simple as that? Its just I've noticed how everything including primitives like int are inheriting the object class so does that mean outside of things that have been explicitly marked as struct are reference?
A related question is around 15 years ago I learned a little bit of c++ and I learned that if anything is bigger than the size of a pointer it should be passed by reference to avoid the overhead of copying it.
As primitives like int in c# are slightly larger than a primitive in c++ being an object instance should we be using ref, in and out wherever possible? I can see the value of using in wherever possible because so far in my learning it feels like that is the closest we have to passing const.
Thank you very much for your time and help.
27 replies
✅ Passing back to the main thread
Hiya
I want to keep track of the amount of milliseconds that have passed in my game. So I've created a thread and told it to run this function.
private void RunTimeUpdater()
{
const int SLEEP_TIME = 50;
long time = 0;
while (true)
{
DateTime currentTime = DateTime.UtcNow;
TimeSpan epochTimeSpan = currentTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
time = (long)epochTimeSpan.TotalMilliseconds;
_model.RecieveNewTime(time);
Thread.Sleep(SLEEP_TIME);
}
}
_model is the root of a composite tree and each branch can handle different things based on how much time since its creation. However doing it this way means that this thread is propagating through my composite and I would much rather have the main thread do it. I have tried using events, task<> and delegates but checking the thread name it always seems to never pass the work off back to the main thread. Is this something that can be done or should I not worry about another thread going through my tree?
Thank you for your help.5 replies