AdamsLair / duality

a 2D Game Development Framework
https://adamslair.github.io/duality
MIT License
1.4k stars 289 forks source link

Change code style to use the newer features up to C# 7.3 #742

Open Barsonax opened 5 years ago

Barsonax commented 5 years ago

Summary

Now that we can use C# 7.3 we can make use of the new features. To name a few:

And many more (we had a nice list somewhere but I lost it). Some of these are currently explicitly blocked because of the .editorconfig. Consider changing these style rules to make use of the newer features.

Documentation for the .editorconfig can be found here https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2019

Note: this issue is for discussing code style changes. These shouldn't change the meaning of the code but make it nicer to write.

Analysis

Barsonax commented 5 years ago

The ?. operator can be used to easily add null checks to your code. Consider this duality code:

if (this.PackageLicenseAcceptRequired != null)
    this.PackageLicenseAcceptRequired(this, args);

Which using the ?. operator can also be written as:

this.PackageLicenseAcceptRequired?.Invoke(this, args);
Barsonax commented 5 years ago

Out variables can now be inlined. Consider this duality code:

bool agreed;
if (!this.licenseAccepted.TryGetValue(package.Name, out agreed) || !agreed)

Which can now be written like so:

if (!this.licenseAccepted.TryGetValue(package.Name, out bool agreed) || !agreed)