Giter VIP home page Giter VIP logo

csharp11_extended-nameof-scope's Introduction

Csharp11_extended-nameof-scope

In C# 11, the extended nameof operator was introduced as a new language feature. The nameof operator allows you to obtain the name of a variable, type, or member at compile-time as a string. The extended nameof operator enhances this functionality by allowing you to get the name of a member from an outer scope, including the containing type.

Here's an example to illustrate how the extended nameof operator works:

using System;

class MyClass
{
    private int myField;

    public void MyMethod()
    {
        Console.WriteLine(nameof(myField));  // Output: myField
        Console.WriteLine(nameof(MyMethod)); // Output: MyMethod
        Console.WriteLine(nameof(MyClass));  // Output: MyClass

        Console.WriteLine(GetOuterMemberName()); // Output: MyMethod
    }

    private string GetOuterMemberName() => nameof(MyMethod);
}

class Program
{
    static void Main()
    {
        MyClass myObj = new MyClass();
        myObj.MyMethod();
    }
}

In the above example, the nameof operator is used to obtain the names of the myField field, MyMethod method, and MyClass class. These names are resolved at compile-time, so if you were to change the actual names of these members, the nameof expressions would still refer to the original names.

Additionally, the GetOuterMemberName method demonstrates how to use the extended nameof operator to retrieve the name of a member from an outer scope. In this case, it retrieves the name of the MyMethod method from within the MyClass type.

The extended nameof operator is useful in scenarios where you need to refer to members by name as strings, such as when working with reflection, debugging, or implementing certain patterns.

csharp11_extended-nameof-scope's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.