In Unity 2017 C# 6 was included as an experimental feature. Now in 2018 Unity has default support for C# 6, and I’m looking forward to taking advantage of these long-awaited features. I like any changes that allow for cleaner, easier to read code and this update delivers exactly that. Here are the features I’m most looking forward to using. Below are examples and use cases for each one:
using static
Using static imports the static members of a class into another class.
- using static UnityEngine.Random;
int rand = Range(1, 100);
This is a good way to reduce writing unnecessary fully qualified names over and over. Reducing clutter is great, and any issues with ambiguous calls can be resolved by fully qualifying the members.
readonly auto properties
Allow you to create a read only property without the extra declaration of a private variable.
- public string FirstName { get; }
This is a nice little reduction of code for immutable variables. I only wish Unity had a system to allow these readonly variables to be serialized, but adding the extra line back in for that case is no difficult matter.
Auto property initializer
Set the initial value of a property in the same line as the declaration.
- public string product {get;} = “Orange”
- public float price {get; set;} = 1f;
A nice change on par with the readonly auto properties, this also removes the unnecessary line to make code more concise.
Null conditional operator (?.) and null coalescing operator (??)
Cleaner, easier null checks and easier handling for null cases
- string firstName = person?.firstName ?? “Not Set”;
Great for invoking delegates and events, or anywhere you perform null checks and handling.
String interpolation
Allows you to embed expressions in a string
- Public string FullName => $”{FirstName} {LastName}”;
Another nice code reduction. Often you will create a function to put some variables together in a string for some kind of human readable data. This can often be simplified to a single line, so why take up more lines than necessary?
nameof
Gets the name of a symbol
- Debug.Log(“This variable had an issue ” + nameof(problemVar));
I always try to remove hardcoded strings of any kind in projects, so this is a welcome change. If you are ever in a situation where you need to pass the name of a method or variable for any reason, using the nameof method is infinitely better than just hardcoding a string. When you attempt to find all references to a method or variable, you will find the object used for the nameof call in that list. This is great for refactoring to ensure all calls continue going to the correct method, and for debugging to track down the possible causes for triggering a method call.
I didn’t cover all the changes, If you’d like to view more changes you can check out the Microsoft article about C# 6 (https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-6). If you use Visual Studio, make sure to change the project language to C# 6 as that isn’t the default setting.