value (C# リファレンス)value (C# Reference)
コンテキスト キーワード value
は、set
アクセサーの property と indexer 宣言で使用されます。The contextual keyword value
is used in the set
accessor in property and indexer declarations. これは、メソッドの入力パラメーターに似ています。It is similar to an input parameter of a method. value
という単語は、クライアント コードでプロパティまたはインデクサーに割り当てる値を表します。The word value
references the value that client code is attempting to assign to the property or indexer. 次の例の MyDerivedClass
には、Name
というプロパティがあります。このプロパティは value
パラメーターを使用して、バッキング フィールド name
に新しい文字列を割り当てます。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 (!string.IsNullOrEmpty(value))
{
name = value;
}
else
{
name = "Unknown";
}
}
}
}
詳細については、プロパティとインデクサーに関するページを参照してください。For more information, see the Properties and Indexers articles.
C# 言語仕様C# language specification
詳細については、「C# 言語の仕様」を参照してください。For more information, see the C# Language Specification. 言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。The language specification is the definitive source for C# syntax and usage.