C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PropertyInt: In the first property's set accessor, the value parameter is written to the console.
PropertyString: Here the value is a string. It is checked for a valid state. The program writes the values.
Tip: You can assign the "value" parameter to a new value. We can change a "value" of null to an empty string literal.
NullEmpty StringC# program that uses value keyword
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);
}
}
Output
5
1
test
Also: It might help with understanding how the C# language works to investigate how "value" is implemented here.
Properties: These are actually compiled into methods such as Set_PropertyInt and Get_PropertyInt.
ILThen: The set method has a regular formal parameter list. The formal parameter has the identifier "value".