Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

C# 6 New Features - nameof Expressions

4/5/2015

Comments

 
C# 6.0 is the new version which will be shipped with Visual Studio 2015 and with new version Microsoft came up with lot many features. So if you haven't tried the new IDE, download the preview version and you can enjoy all the new features which will be coming in the  release.
Below are some of the features :

Today we will learn about the new feature called "nameof" expression which will help developers solve the problem while coding with Properties and Notifications.

Often Developer requires to provide the string that names which names some program element while throwing the exception and even Developer face issues with the hard coded string literals when passing as a parameter to Property Change event handler. Using string literal is simple but its highly error prone and there are high chances that Developer may spell it wrong.

Prior to C# 6.0 it was :

But now Developers don't have to specify any string literals and the savior for them is the keyword named "nameof" which developer can use to create the expression to specify the name.

So the above code will change as below :

private CountryBO _country;
        public CountryBO Country
        {
            get { return _country;}
            set
            {
                _country = value;
                OnNotifyPropertyChanged("Country");
            }
        }
But now Developers don't have to specify any string literals and the savior for them is the keyword named "nameof" which developer can use to create the expression to specify the name.

So the above code will change as below :
private CountryBO _country;
        public CountryBO Country
        {
            get { return _country;}
            set
            {
                _country = value;
                OnNotifyPropertyChanged(nameof(Country));
            }
        }
Thus "nameof(Country)" will return string literal "Country". Hope you like the features.
In my next post I will explore more of the C# 6.0 features which will be helpful to the Developers. Happy Coding.

Rishi
Comments
comments powered by Disqus

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot