Saving Re-Writing Code Since C# 4
When building your own systems, you might sometimes require a method you just made to slightly change its functionality in some cases.
It might be that you wish you could provide an extra boolean or number to that method, without breaking all you have already done.
In these cases, what you are looking for are optional parameters, something already built-in in C#:
- Write down your method.
- When declaring the parameters it should receive, make sure the parameters you want to make optional are ALL located at the end of the list.
- Assign a value to each of those parameters you want to make optional. (That value will be the “default”.)
- When calling the method, cover all non-optional parameters, and use as many optional parameters as you want.
What you just read about was ONE of several programmer tips that are available on the Programmer Tips Wiki.
So if you liked the one you just read, be sure to check it out (or star it) as it gets updated often! 😮Read More Programmer Tips!
I love optional parameters, especially in game development. For example, I can create an instance of an object in a scene, and via optional parameters set the default position and rotation to 0, but leaving the option to specify them. It’s magic.
So I hope knowing this becomes useful to someone else.
That Is All For Today, But Like Always…
Thank you very much for reading my blog :3
Do you make games? Then I got something for you.
Given how you are reading about Unity tools, just wanted to extend you an invitation to subscribe to the GAME DEVELOPER newsletter for FREE.
Don’t worry, you will ONLY receive notifications on game development content, which include helpful game development tutorials, tips and tricks, and updated on new FREE dev tools directly on your inbox.