Home >Backend Development >C++ >How Can I Retrieve the Original Variable Name Passed to a C# Function?

How Can I Retrieve the Original Variable Name Passed to a C# Function?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-29 17:46:10481browse

How Can I Retrieve the Original Variable Name Passed to a C# Function?

Get the original variable name passed to the function

In C#, it is very common to pass variables as parameters as parameters. However, obtaining the original name of the transmission variable may be tricky. Consider the following example:

When using variable calls
<code class="language-csharp">public string ExampleFunction(string Variable)
{
    return "something"; // 修正:添加返回值
}

string WhatIsMyName = "Hello World";
string Hello = ExampleFunction(WhatIsMyName);</code>
, we may want to get the name of the original variable, for example:

WhatIsMyName ExampleFunction Can this be implemented in C#?

<code class="language-csharp">Variable.OriginalName.ToString() // == "WhatIsMyName"</code>
Although the original variable name cannot be obtained directly, the C# 3.0 and higher versions can use LAMBDA expressions to provide a solution:

Note:

This method depends on unspecified behavior and may not be compatible with the future version or other compilers of C#. It is only suitable for direct access variables. If the variables pass or conversion through any calculation or conversion, this method will fail. In addition, this technology is mainly used for reflection and code analysis, and it is not recommended to use in conventional code.

The above is the detailed content of How Can I Retrieve the Original Variable Name Passed to a C# Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn