Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:54:31+00:00 2026-05-14T05:54:31+00:00

For example, static void Main() { var someVar = 3; Console.Write(GetVariableName(someVar)); } The output

  • 0

For example,

static void Main()
{
    var someVar = 3;

    Console.Write(GetVariableName(someVar));
}

The output of this program should be:

someVar

How can I achieve that using reflection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:54:31+00:00Added an answer on May 14, 2026 at 5:54 am

    It is not possible to do this with reflection, because variables won’t have a name once compiled to IL. However, you can use expression trees and promote the variable to a closure:

    static string GetVariableName<T>(Expression<Func<T>> expr)
    {
        var body = (MemberExpression)expr.Body;
    
        return body.Member.Name;
    }
    

    You can use this method as follows:

    static void Main()
    {
        var someVar = 3;
    
        Console.Write(GetVariableName(() => someVar));
    }
    

    Note that this is pretty slow, so don’t use it in performance critical paths of your application. Every time this code runs, several objects are created (which causes GC pressure) and under the cover many non-inlinable methods are called and some heavy reflection is used.

    For a more complete example, see here.

    UPDATE

    With C# 6.0, the nameof keyword is added to the language, which allows us to do the following:

    static void Main()
    {
        var someVar = 3;
    
        Console.Write(nameof(someVar));
    }
    

    This is obviously much more convenient and has the same cost has defining the string as constant string literal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think I see the problem here. It's to do… May 17, 2026 at 12:45 am
  • Editorial Team
    Editorial Team added an answer No. Django seems closer to a CMS then "ASP.NET MVC"… May 17, 2026 at 12:45 am
  • Editorial Team
    Editorial Team added an answer Used decorators May 17, 2026 at 12:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I need some help adding conditions to a linq query. Take this small example
I'm reading manning book about LINQ, and there is an example: static class QueryReuse
I am referring to this code example, which is being reported in https://bugs.java.com/bugdatabase/view_bug?bug_id=6254531 import
I'm currently writing about dynamic typing, and I'm giving an example of Excel interop.
Given a group like this in Active Directory: MainGroup GroupA User1 User2 GroupB User3
Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application?
From what I understand about the where clause in LINQ, it combines elements from
I am using Ninject to load several modules. When two modules try to bind
I have a trivial .Net 2.0 SOAP web service. I want to access it
In the ContainsIngredients method in the following code, is it possible to cache the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.