TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Value Keyword

This C# example program shows the value keyword. Value has a special meaning in properties.

Value is used in properties.

In the C# language, we need to declare a type for a parameter in a parameter list. With "value" in property setters, we omit this. The type of value is determined by the enclosing property type.

Example. In this program, each property has a set accessor that uses the value parameter. In the first property, the value parameter is written to the console. In the second, the value is a string. It is checked for a valid state.

Console.WriteLine

C# 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

You can assign value. This is not common, but you can assign the "value" parameter to a new value. So if you want to change a "value" of null to an empty string literal in the second property above, this is possible.

NullEmpty Strings

Discussion. The C# specification refers to the value parameter in its description of Accessors in section 10.7 Properties. On page 481, it states "The implicit parameter of a set accessor is always named value."

Also, it might help with understanding how the C# language works to investigate how "value" is implemented here. Properties are actually compiled into methods such as Set_PropertyInt and Get_PropertyInt.

Intermediate Language

Then: The set method has a regular formal parameter list. The formal parameter has the identifier "value".

Summary. In this example, we examined the value parameter in properties. We looked at a code example, referenced the language specification. We then discovered the underlying implementation of the value parameter in properties.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf