With parameters in a property, we can use the special keyword "value." This way we do not need to specify the type. The type is determined by the enclosing property type.
An example. In this program, each property has a set accessor that uses the value parameter. We see 2 properties, PropertyInt and PropertyString.
Start In the PropertyInt set accessor, the value parameter is written to the console.
using System;
class Program
{
int PropertyInt
{
get
{
return 1;
}
set
{
Console.WriteLine(value);
}
}
string _backing;
string PropertyString
{
get
{
return this._backing;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
this._backing = value;
}
}
static void Main()
{
Program program = new Program();
// Use PropertyInt.
program.PropertyInt = 5;
Console.WriteLine(program.PropertyInt);
// Use PropertyString.
program.PropertyString = "test";
Console.WriteLine(program.PropertyString);
}
}5
1
test
A discussion. The C# specification refers to "value" in its description of Accessors in Properties. On page 481, it states "The implicit parameter of a set accessor" is always value.
Also It might help with understanding how the C# language works to investigate how "value" is implemented here.
Detail These are actually compiled into methods such as Set_PropertyInt and Get_PropertyInt.
Then The set method has a regular formal parameter list. The formal parameter has the identifier "value."
A summary. We examined the value parameter in properties and referenced the language specification. We then discovered the underlying implementation of the value parameter in properties.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 22, 2023 (edit).