I need to design a class where one property name has to be return
, but when I create a property name like return
then I get an error.
After some research I found out that one can use a reserved keyword as a property or variable name by adding a @
prefix in C#, or by enclosing 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 error, but when I try to access property name like return
then I need to write the name like @return
, which I don't want. I want to access the property name like p.return = "hello";
instead of p.@return = "hello";
so I'd like to know if there is any way to do that?
You can't. It is a reserved keyword. That means "you can't". Contrast to "contextual keywords" which usually means "we added this later, so we needed it to work in some pre-existing scenarios".
The moderate answer here is: use @return
.
A better answer here is: rename your property. Perhaps ReturnValue
.
There is also the option of, say, Return
- but you might need to think about case-insensitive languages too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With