Hi there,
I am recently new to C#, and have a question. What is with, or why do we need to, and or how do we properly know when and how to apply return new, return yield, return false, return true and or return?
I have it down pretty good, but it has been mostly thru trial and error. I am not sure the logic behind it nor its purpose.
yeah he pretty much covered it, its to cut a function short early.
if (thisTrue) return true;
Would only appear in a function that was declared as a boolean
while (thisTrue) return null;
Could appear in many type of function, eg if the function was for calculating a Vector3, it could end prematurely and say that the Vector3 is null, not valid
More specifically - yield returns a result but maintains a position in code so that it can be returned to later. Collections can use this in a foreach because they implement IEnumerable which is also why Coroutines in Unity must return an IEnumerator type. Typically you yield return null in Unity when you want to wait a single frame. Returning null allows you to do this without allocating memory.
Here is the most base way to think about it in my opinion.
Objects have a type, right? Transform, GameObject, List, even Object. Think of a Type as a class definition. So when you declare a class like this:
public class SomeClass
{
}
You’ve also declared a Type of SomeClass.
A method also has a Type. The type can be void which means essentially “no type”. A method must return a value whose type matches its type. You don’t have to explicitly return anything in methods “of type” void because it’s implied when the end of the method is reached.
// GetOne "has a type of" int
public int GetOne()
{
return 1; // return an integer because we have to
}
// DoStuff "has a type of" void or "nothing"
public void DoStuff()
{
transform.position = Vector3.zero;
// we don't have to return anything here
// because we reached the end of the method
// so a return is implied (the code will jump back
// to whatever method called this one)
}
// this is functionally equivalent
public void DoStuff()
{
transform.position = Vector3.zero;
return;
}
// however, we can also use return to "early out"
public void DoStuff()
{
transform.position = Vector3.zero;
if (someBool)
return;
// this code won't execute if someBool == true
// because we "returned" or "exited" the method
// before it was reached
transform.rotation = Quaternion.identity;
}
I get return, yield etc and how they are used, but coming from JS where the former are all we need, where as in c# we need to add, in general, return true, or return false, or return null etc.
I’m not sure I understand what you mean. You would return true or false if the method requires a boolean value to be returned. You can return null if the method requires a reference type to be returned.
Except that it is required if your function needs to return a value, no matter what language you are using.
And if you declare your C# functions as void, you don’t need to return anything (note that MonoBehaviour functions like Update are void and have no return values).