Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

nameof Expressions


nameof Expressions:

The nameof operator allows developers to use a program element as text. Now we cannot specify any string literals directly with the nameof operator. It can be used to create the nameof expression to specify the name where expression may be a property-group or a method-group.


C# 5.0 (VS 2012)

public class Student
    {
        public Student(string teacher)
        {
            if (teacher == null) throw new Exception("teacher");
            TeacherName = teacher;
        }
        public string TeacherName { get; set; }
    }


C# 6.0 (VS 2015)

public class Student
    {
        public Student(string teacher)
        {
            if (teacher == null) throw new Exception(nameof(teacher));
            TeacherName = teacher;
        }
 
        publicstring TeacherName { get; set; }
    }

   < > 

Invalid entry,please enter valid data.

Loading