Fun tricks with C# function arguments

So with Ludum Dare rapidly approaching, now is as good time as any to do one thing I, Taro Omiya, always wanted to do here on this blog…but never got around it. I’m going to talk about neat scripting tricks you can do with Unity 3D’s C# scripting! Hopefully these tricks and trips will improve your programming skills.

In pretty much all these tips, I’m going to assume you have some programming experiences (especially Java), but don’t know some advanced C# stuff.

Anyway, for this completely sporadic C# tips, let’s talk about functions. Specifically, function arguments. Programmers with Java experience will know that unless the argument is an object, you cannot change the argument within a function.

This is due to scoping: the value, y is copied into argument x before being set within the function, so no changes are being made to y itself. The Java work-around for this is, naturally, wrapping the variable with an object.

Fortunately, such a hack isn’t necessary in C#. Instead, you can use ref:

ref allows the variable y to be passed in as a reference. In short, it lets changes in the function be reflected outside of the function’s scope. As part of making it clear in code that a variable is passed in as a reference, ref must be typed in when passing a variable into the function.

A similar modifier to the ref is out. Unlike ref, out arguments must set their value within the function, and cannot be accessed before this happens. This restriction is useful when what you really want from a function argument is an extra return variable.

There are two other tricks Unity’s C# has that Java doesn’t. For example, take this classic Java overriding mechanism.

In the example, there are two different OverrideDemo() functions: one that takes a argument in and prints that into the console, and the other which doesn’t take an argument, but instead, calls the one that does and tosses in 0.

This can be shortened. It’s possible to create optional arguments:

Optional arguments can created by just adding an = sign, along with a constant, a new object declaration, or the result of default([type]). If the argument is missing, the value to the right of the = sign will be used instead. Note that all optional arguments must come last (unless you have a variable argument; then it comes second-to-last).

Incidentally, you can also set an optional argument by providing the name of the argument. This is useful if you need to set an argument in the middle of a series of optional arguments:

By simply providing a [name]:, you can assign to the intended argument (in the above example’s case, y is being set to 4).

Last but not least is variable argument. Java actually already has this in the form as follows:

This allows inputting in as many strings into the function’s arguments as you want. C# is very similar, but with a slightly different syntax:

Instead of just ..., C# syntax uses params type[], where type is the type of arguments the function accepts. As mentioned earlier, params must absolutely come last, even farther than the optional arguments.

And that’s it! Happy programming!

No Comments

Have something to say? Leave a comment!

We use cookies to ensure that we give you the best experience on our website.