How to use C # reserved keyword as property name without @ prefix?

I need to create a class where one property name should be return , but when I create a property name like return , I get an error.

After some research, it turned out that you can use the reserved keyword as the name of a property or variable by adding the @ prefix in C # or by including it in square brackets [] in VB.NET. For example:

 var @class = new object(); 

So here is my class design code.

 public class Person { string _retVal; public string @return { get { return _retVal; } set { _retVal = value; } } } ... Person p = new Person(); p.@return = "hello"; 

Now I am not getting any errors, but when I try to access the property name, for example return , then I need to write a name like @return , which I do not want. I want to access the property name, for example p.return = "hello"; instead of p.@return = "hello"; so I would like to know if there is a way to do this?

+7
source share
3 answers

You can not. This keyword is reserved . This means "you cannot." Contrast to "contextual keywords," which usually means "we added this later, so we needed to work in some pre-existing scripts."

Moderate answer: use @return .

The best answer here: rename your property. Perhaps ReturnValue .

There is also an option, say, Return - but you might have to think about case-insensitive languages.

+25
source

You can rename namespaces as follows:

 using Test = System.Diagnostics; 
0
source

This cannot be achieved because reserved keywords are predefined, reserved identifiers that have special meanings for the compiler.

it’s better to change the name of the property and use it in your code ... something, as indicated by @Marc's answer ...

-2
source

All Articles