How do I use a C# keyword as a property name?
You can use keywords in C# as identifiers by prepending @ infront of them.
var @class = new object();
To quote from the MSDN Documentation on C# Keywords:
Keywords are predefined, reserved identifiers that have special
meanings to the compiler. They cannot be used as identifiers in your
program unless they include @ as a prefix. For example, @if is a valid
identifier but if is not because if is a keyword.
How do I use a C# reserved keyword as a property name without the @ prefix?
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.
Why I can't put a property name in setter instead of value keyword?
The following assumption about your property -
which should hold the same assigned value
is basically wrong. Your property itself is not holding any value at all, it just returns the value of the variable i
.
To get a clear picture, keep in mind that C# properties are just syntactic sugar over a pair of methods, and for your property -
public int prop
{
get { return i; }
set { i = value }
}
two methods will be generated behind the scene, which look like -
public int get_prop()
{
return this.i;
}
public void set_prop(int value)
{
this.i = value;
}
As you can see, the value
keyword in your property represents a generalized name for the parameter that is passed from outside when you try to set value to a property. So, when you do something like -
p.prop = 5;
basically the set_prop
method gets called with 5 as the value for the parameter value
.
Now, when you are trying to use your property like -
public int prop
{
get { return i; }
set { i = prop }
}
the generated methods will look like -
public int get_prop()
{
return this.i;
}
public void set_prop(int value)
{
this.i = this.prop;
}
and as you can see, this code is totally ignoring the value of the value
parameter passed from outside.
Your code is still setting the value though. It is calling the get method (which returns the value of i
) and setting the already set value of i
to i
again. It is just not making any use of the value that has been passed to it.
C# default Write Property name
Basically, no. It is a language feature that value
is the contextual keyword that always represents the incoming value in a set
accessor (and in custom event accessors).
You can fake it:
var newName = value;
but... you get better syntax highlighting with value
than newName
, and any C# reader automatically knows what value
represents in that context.
How to define a Property that it's the keyword like 'checked'?
If you want to use a reserved word as an identifier (which you should avoid anyway), prefix it with the @
symbol. So your code would be:
public class Node
{
public bool @checked
{
get;set;
}
}
Unfortunately, this also means you will need to use the @
symbol when referencing the property. It also won't appear in Visual Studio's IntelliSense listing.
Use 'class' (or other reserved keyword) as property on anonymous type
Can You try with escaping the class with : @.
So, please modify your code to :
<%: Html.ActionLink("Click Here", "Action", null, new {@class="myClass"})%>
Related Topics
Parameters Passed by Reference Come Back Garbage Using P/Invoke
Difference Between Equals/Equals and == Operator
How to Get Object Size in Memory
Why Do Assignment Statements Return a Value
When to Use Cast() and Oftype() in Linq
How to Load an Assembly to Appdomain with All References Recursively
Load an Exe File and Run It from Memory
.Net Core MAChine Key Alternative for Webfarm
Why Doesn't .Net/C# Optimize for Tail-Call Recursion
Serialize Class Containing Dictionary Member
How to Determine the Owner of a Process in C#
How to Assign a Base Class Object to a Derived Class Reference with an Explicit Typecast
Reading Email Using Pop3 in C#
How Using Try Catch for Exception Handling Is Best Practice
Change Current Linux User in a C# Application Running with Mono
How to Change a Private Readonly Field in C# Using Reflection