"ValueTask Example"

// If we assume most users are not logged in, we can avoid allocating a Task object every time we invoke this method for most cases
// In the case where the user IS logged in, we wrap an actual Task<int> which will then be deferred to
public ValueTask<int> GetUserIDAsync(User u) {
    if (!u.IsLoggedIn) return ValueTask.FromResult(0);
    else return new ValueTask<int>(FetchUserIDFromDatabaseAsync(u)); // Assume FetchUserIDFromDatabaseAsync() returns a Task<int>
}


Code snippet taken from "Two Decades of C#: A Reference - C# 7".