Purpose of return false, new, null, true....

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.

I’m mildly confused by what you are asking. In general, you must return an object of the type which your function is declared as returning.

i.e.

public int foo(){ /*code*/ }

MUST return an int.

No, if I ask,

if (thisTrue)
return true;


///or..
while (thisTrue)
yield return null;

Etc.

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

New = Creates a new instance of an object so if you say “return new Class();” it would return a new object. this is mainly used on refference types. http://msdn.microsoft.com/en-us/library/51y09td4(v=vs.71).aspx
Return = Return a type from a method. http://msdn.microsoft.com/en-us/library/1h3swy84(v=vs.71).aspx
Yield = Returns one item at a item to a collection, yield is a little slow to use versus just returning a whole collection. http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.80).aspx

If you’re whole new to C# and would like some tutorials head over to my thread here: http://forum.unity3d.com/threads/189833-C-Guides-Tutorials-for-everyone! I put updates up for it weekly, and I’m working on a method chapter atm.

That site is pretty good!

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.

Wow.
Seems so deep, for lack of a a better term.

Thanks. I will go over this.
Best.
Ren

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.

This is what I am talking about.

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.

It’s the exact same whether you’re coding in C#, UnityScript, or Boo.

You return true, false, null, a string, an integer, an array, or whatever you need to return depending on what you want your function to accomplish.

Only difference in C#, is you have to manually state what the return value of the function will be, and if it returns nothing, you specify it as void.

@Kelso and tonyd,
yah, that pretty much explains it.

Coming from JS, it was never a factor, so being somewhat new to C#, I was not sure why. So, return needs a value of what it is returning.

So tests will show, but for jokes sake, does it mess with a program if …

if(thisTrue)
return false;

It is a factor in Js

function somefunction : Vector3 () {
 return true //This is bogus
}

its not required.

maybe does not hurt, but will not cause an error.

You can do whatever you want. Here’s an example

public bool CanAttack()
{
    if (isFriendly)
       return false;

    return true;
}

Which is silly because you could simply do

public bool CanAttack()
{
    return !isFriendly;
}

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).

    void Start (){
        SayHello();
    }
    
    void SayHello(){
        Debug.Log("Hello World");
    }

Try doing this, and it will give you a similar error to what you’d see in C#.

function Sum(n1 : int, n2 : int){
     var theSum = n1+n2;
}

function Start(){
     var result = Sum(1, 2);
}

It expects us to return something, in this case you’d want to return theSum for the function to work correctly.