Repository: microsoft/garnet
File: libs/server/ACL/UserHandle.cs (C#)

Code before:
﻿// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Threading;

namespace Garnet.server.ACL
{
    /// <summary>
    /// A reference used to access a <see cref="User"/>.
    /// </summary>
    public class UserHandle
    {
        /// <summary>
        /// The <see cref="User"/> referred to by the <see cref="UserHandle"/>.
        /// </summary>
        public User user;

        /// <summary>
        /// Constructor for a <see cref="UserHandle"/>.
        /// </summary>
        /// <param name="user">The <see cref="User"/> the handle will reference.</param>
        public UserHandle(User user) => this.user = user;

        /// <summary>
        /// Returns the current version of the <see cref="User"/> with the latest modifications.
        /// </summary>
        /// <returns>Returns the current version of the <see cref="User"/> with the latest modifications.</returns>
        public User User
        {
            get => this.user;
        }

        /// <summary>
        /// Attempts to set the <see cref="User"/> for a handle.
        /// </summary>
        /// <param name="newUser">A <see cref="User"/> that should secede the existing <see cref="User"/> referred to by the handle.</param>
        /// <param name="replacedUser">The <see cref="User"/> expected to be replaced.</param>
        /// <returns>True if the assignment was performed; otherwise false.</returns>
        public bool TrySetUser(User newUser, User replacedUser)
            => Interlocked.CompareExchange(ref this.user, newUser, null) == null ||
                Interlocked.CompareExchange(ref this.user, newUser, replacedUser) == replacedUser;
    }

Review comment (suggestion):
Can `user` ever start as null, or transition to null? If yes, what is the scenario for that? If no, can we assert in the constructor that user is non-null.

Code after:
﻿// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using System.Threading;

namespace Garnet.server.ACL
{
    /// <summary>
    /// A reference used to access a <see cref="User"/>.
    /// </summary>
    public class UserHandle
    {
        /// <summary>
        /// The <see cref="User"/> referred to by the <see cref="UserHandle"/>.
        /// </summary>
        public User user;

        /// <summary>
        /// Constructor for a <see cref="UserHandle"/>.
        /// </summary>
        /// <param name="user">The <see cref="User"/> the handle will reference.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="user"/> is <c>null</c>.</exception>
        public UserHandle(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            this.user = user;
        }


        /// <summary>
        /// Returns the current version of the <see cref="User"/> with the latest modifications.
        /// </summary>
        /// <returns>Returns the current version of the <see cref="User"/> with the latest modifications.</returns>
        public User User => user;

        /// <summary>
        /// Attempts to set the <see cref="User"/> for a handle.