value(C# 参考)

上下文关键字 value属性索引器声明的 set 访问器中使用。 此关键字类似于方法的输入参数。 关键字 value 引用客户端代码尝试分配给属性或索引器的值。 在以下示例中,MyDerivedClass 有一个名为 Name 的属性,该属性使用 value 参数向支持字段 name 分配新字符串。 从客户端代码的角度来看,该操作写作一个简单的赋值语句。

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 (!string.IsNullOrEmpty(value))
            {
                _name = value;
            }
            else
            {
                _name = "Unknown";
            }
        }
    }
}

有关详细信息,请参阅属性索引器这两篇文章。

C# 语言规范

有关详细信息,请参阅 C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。

另请参阅