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#:
Simple, Yet Powerful
- 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.
Get More Useful Tips On The GitHub Wiki
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.