#1,209 – C# 6.0 – Using the nameof Operator

C# 6.0 adds a nameof operator that accepts the name of some program element and returns a string representing the name of the element.  The nameof operator can take as a parameter:

  • Class names
  • Class members
  • Method names
  • Variables

Below, the ShowoffNameof method reports the names of various program elements within the Dog class.  This avoids having to explicitly use reflection to access the names of things.

    public class Dog
        public string Name { get; set; }

        public Dog(string name)
            Name = name;

        public string ShowoffNameof(int myInteger, string myString)
            StringBuilder sbOut = new StringBuilder();

            sbOut.AppendLine(string.Format("Class: {0}", nameof(Dog)));
            sbOut.AppendLine(string.Format("Method: {0}", nameof(ShowoffNameof)));
            sbOut.AppendLine(string.Format("Name prop: {0}", nameof(Name)));
            sbOut.AppendLine(string.Format("1st param: {0}", nameof(myInteger)));
            sbOut.AppendLine(string.Format("2nd param: {0}", nameof(myString)));
            sbOut.AppendLine(string.Format("Local variable: {0}", nameof(sbOut)));

            return sbOut.ToString();

Here’s the output:


#1,203 – C# 6.0 – Using the Null-Conditional with Value Types

The new null-conditional allows checking for null and de-referencing a reference-typed variable in a single step.

            string sLeft = sTest?.Substring(0, 1);

If the variable being checked for null (e.g. sTest) is null, the result of the expression is null.  This works as expected if the result of the expression is being assigned to a reference-typed variable (e.g. sLeft).  If the method or property being invoked, however, is a value type, then the result of the expression must be a nullable type.

For example, we might do the following in C# 5.0:

            int sLen;
            if (sTest != null)
                sLen = sTest.Length;

If sTest is null, the assignment isn’t done and sLen retains its value.

In C# 6.0, you can do this assignment using the null-conditional.  The variable being assigned to must be a nullable type whose base type matches the type of the expression.

            int? sLen = sTest?.Length;

#1,202 – C# 6.0 – Null-Conditional Operator

One of the new features coming in C# 6.0 is the null-conditional operator.

If you have a reference-typed variable, it can have a null value or can refer to instance of the appropriate type.  You’ll often see the following pattern, checking for null before invoking a method on the object, to avoid a NullReferenceException.

            string sTest = DateTime.Now.Hour == 5 ? "Five" : null;

            // OLD: Check for null to avoid NullReferenceException
            string sLeft;
            if (sTest != null)
                sLeft = sTest.Substring(0, 1);

The null-conditional operator allows us to do the same check for null, but in a more concise manner.

            // NEW: Null-Conditional operator
            sLeft = sTest?.Substring(0, 1);

If sTest is non-null, the Substring method is called and the result is assigned to sLeft.  If sTest is null, the expression returns null, so a null value is assigned to sLeft.