Expression-Bodied Members to Reduce Your Code Verbosity
Continuing with our Modern C# for Unity Game Development series, today I'm going to show you how to reduce the verbosity of your code with C# Expression-Bodied Members.
[Visit the original blog post at Unity C#: Expression-Bodied Members to Reduce Your Code Verbosity]
Continuing with our Modern C# for Unity Game Development series, today I'm going to show you how to reduce the verbosity of your code with C# Expression-Bodied Members.
Note: this might upset some verbosity lovers out there ðŸ˜Â
Table of Contents
1 What's Wrong With This Damn Code?
2 Level Up With Unity C# Expression-Bodied Members
What's Wrong With This Damn Code?
Imagine you want to keep your player data synchronized with a server.
(Such a rare scenario, right?)
So you create your user manager class that does a GET request to update some fields.
Basically, you ask your backend for information on the a player ID. With that new information, you update your UI, gameplay elements and so on.
Here's a simplified code snippet that does this update:
public class UserDataProvider
{
private string UrlUserInfo
{
get { return $"https://mygame.api/user/{_userData.userId}"; }
}
UserDataProvider(string userId)
{
_userData = new UserData(userId);
}
void Start()
{
CoroutineManager.StartCoroutine(RefreshUserInfo());
}
IEnumerator RefreshUserInfo()
{
var request = UnityWebRequest.Get(UrlUserInfo);
yield return request.SendWebRequest();
_userData.name = ParseJson(request.downloadHandler.text, "name");
// ...
}
}So, what's the problem with that?
You see, verbosity is my problem.
Sure, it looks quite normal if you're used to it... (I got pretty used to this code-writing style myself).
However, there's BIG room for improvement...
And today, we will be concerned with two questions:
- Do we really need so many curly braces?
- Do we really need these keywords, such as get and return?
A very standard-driven, verbosity-lover programmer will tell you that you need all of it.
But me, as a programmer who reads thousands of line of code per day, I'll buy into anything that:
- Reduces the amount of time I spend reading code,
- simplifies the code that I read...
- so that I can use my time more efficiently.
And today I'm showing you the tool that will let you achieve all of this.

Level Up With Unity C# Expression-Bodied Members
Expression-bodied members is just one of these fancy terms that are easier to understand by reading code than by trying to decipher its name.
Put simply: this C# feature lets you remove verbosity when dealing with single expressions.
That means:
- If your getter is a single expression, you can simplify it.
- If your constructor is a single expression, you can simplify it.
- If your function is a single expression, you can simplify it.
- And a few more cases alike.
We gain this simplicity by skipping certain language elements (such as curly braces) and introducing the => operator.
No worries, reading actual code will make it much easier to understand.
Remember that getter and the functions?
Well, have a second look now:
private string UrlUserInfo => $"http://mygame.api/user/{_userData.userId}";
UserDataProvider(string userId) => _userData = new UserData(userId);
void Start() => StartCoroutine(RefreshUserInfo());By using expression-bodied members I simplified my code so that...
- The getter is now a single line instead of 4 or even 7.
- The constructor is now 1 line instead of 4
- The Start function is now a single line instead of 4.
We got rid of curly braces... and the return + get keywords.
And I could go on with more examples, but you get the idea.
You can apply this technique as well to finalizers (destructors) and setters.
Here's one last example before I go:
private UserData _userData;
public UserData MyUserData
{
get => _userData;
set => _userData = value ?? _userData;
}Neat!
What's Next?
If you missed any of my other posts on Modern C# for Unity game development, you can quickly catch up here.
And if you're wondering what else you can learn today to substantially improve your game, have a look at my tutorial on improving your game memory and loading times.
Alright, talk to you soon!
~Ruben
Recommended resources
Game Engines
Unreal Engine 5 Best Practices
Amazon · Book
Written by multi-award-winning Unreal generalist Tyson J. Butler-Boschma, Founder and Creative Director of Toybox Games Studios, this book addresses common challenges you face when advancing your expertise in lighting, environment design, and cinematic storytelling.
GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Ruben Torres Bonet (The Gamedev Guru)
Static Batching Like You Never Saw
Most developers think they know how static batching truly works. But that’s a lie: they don’t.
Ruben Torres Bonet (The Gamedev Guru)
Don't Let Offscreen Objects Ruin Your Performance - Unity CullingGroup API
Learn how to make your expensive scripts cost nothing while they are offscreen.
Ruben Torres Bonet (The Gamedev Guru)
Raycast Commands: A New Performance Era
Learn how to stop your current raycasts from stealing performance and upgrade them to the new era of Unity raycast comm…
Ruben Torres Bonet (The Gamedev Guru)
Discussion