Useful C# Features (or “Cool Things You Didn’t Know You Could Use in Unity”)
1. Tuples & Discards! Tuples is something more dynamic languages like JavaScript have been holding over the more static languages for a while. It allows you to do more with less action (higher level ones, that is). While tuples have existed in C# before as a class of their own, they are now getting a proper implementation. It basically makes a complex variable/object without having to predefine it using a class or a struct. You can even use it to return more than one element from a method(!!!). Unfortunately, the Unity editor doesn’t support handling those kinds of returns yet but they are still useful inside your scripts. Discards are those underscores you see in the example below. It allows you to deconstruct a tuple and discard the data you don’t want, only keeping what you actually need in its own named variable. 2. Pattern Matching! Pattern matching allows you to test a runtime type in an if or switch statement and then convert it to that specific type all in a single statement! It also increases the power of switch, allowing you to do a lot more with less and create much more complex testing blocks. 3. Local functions Local functions is currently one of my favorite features in C#. You could already create functions inside functions by assigning a block into an Action or Func variable but now you can just define an anonymous function and not worry about what returns or not returns a value or where Continue Reading →
Posted in IT, No Category, Programming by Eran with comments disabled.