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

  • SEARCH
  • Home
  • 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 7694849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:24:43+00:00 2026-05-31T21:24:43+00:00

I would like to get the name of a variable or parameter: For example

  • 0

I would like to get the name of a variable or parameter:

For example if I have:

var myInput = "input";

var nameOfVar = GETNAME(myInput); // ==> nameOfVar should be = myInput

void testName([Type?] myInput)
{
   var nameOfParam = GETNAME(myInput); // ==> nameOfParam should be = myInput
}

How can I do it in C#?

  • 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-31T21:24:45+00:00Added an answer on May 31, 2026 at 9:24 pm

    Pre C# 6.0 solution

    You can use this to get a name of any provided member:

    public static class MemberInfoGetting
    {
        public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
        {
            MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
            return expressionBody.Member.Name;
        }
    }
    

    To get name of a variable:

    string testVariable = "value";
    string nameOfTestVariable = MemberInfoGetting.GetMemberName(() => testVariable);
    

    To get name of a parameter:

    public class TestClass
    {
        public void TestMethod(string param1, string param2)
        {
            string nameOfParam1 = MemberInfoGetting.GetMemberName(() => param1);
        }
    }
    

    C# 6.0 and higher solution

    You can use the nameof operator for parameters, variables and properties alike:

    string testVariable = "value";
    string nameOfTestVariable = nameof(testVariable);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to get the name of a variable as
I would like to have declaration like this: void Date::get_days_name(const Date& = this) which
I would like to get country, region and city name from longitude and latitude
Is it possible to get the parameter name (where I have parmName below)? Or
I have a c# .net 2.0CF application where I would like to get not
I would like to get the value and the name of the element(or ID)
I would like to know, if anyone can help how to get NODE name
In the example below, what would you name the parameter given that it is
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like to get the most out of working in cch or tcsh

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.