nameof Expression - C# 6 Language New features

Sometimes, we face such situations where we have to write class property names instead of property values, at that time we have to write static string or use various property functions. Instead of writing a static string, Microsoft introduces the new feature nameof expression. You have to just use following code instead of static string.

In example, I have class having name MyClass. I initialize it and call in Program class. In previous version of C# we have to write static string like Console.Writeline("name"). Now, If I want to change property name? This is worse situation where I have to find a static string in a whole project. At this time a new feature is more useful. Instead of using a static string we can use Console.Writeline(nameof( This would be a better approach when we have to change property name.

using System;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            MyClass cls = new MyClass();

    public class MyClass
        public string name { get; set; } = "Bhavik";
        public string email { get; private set; } = null;
        public string firstName { get; set; }
output should be
Don't forgot to read my C# 6 language new features series
Conclusion: nameof expression will more helpful to those developers( just like me) whose clients dramatically ask for to change property names. And, I am pretty sure, name of expression will help to rid of static declaration of property names.


Popular posts from this blog

12 opensource and free MVC gridview contols

Scheduling background jobs using Hangfire in ASP.NET Core

Difference ExecuteNonQuery vs ExecuteScalar vs ExecuteReader in