Dela via


value (C# Reference)

The contextual keyword value is used in the set accessor in ordinary property declarations. It is similar to an input parameter on a method. The word value references the value that client code is attempting to assign to the property. In the following example, MyDerivedClass has a property called Name that uses the value parameter to assign a new string to the backing field name. From the point of view of client code, the operation is written as a simple assignment.

class MyBaseClass
{
    // virtual auto-implemented property. Overrides can only 
    // provide specialized behavior if they implement get and set accessors. 
    public virtual string Name { get; set; }

    // ordinary virtual property with backing field 
    private int num;
    public virtual int Number
    {
        get { return num; }
        set { num = value; }
    }
}


class MyDerivedClass : MyBaseClass
{
    private string name;

   // Override auto-implemented property with ordinary property 
   // to provide specialized accessor behavior. 
    public override string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (value != String.Empty)
            {
                name = value;
            }
            else
            {
                name = "Unknown";
            }
        }
    }

}

For more information about the use of value, see Properties (C# Programming Guide).

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.7.2 Properties

  • 10.7.2 Accessors

See Also

Concepts

C# Programming Guide

Reference

C# Keywords

Other Resources

C# Reference