C# サンプル集 |
Visual Basic 中学校 > C# サンプル集 > C# サンプル集目次 >
プロパティの宣言
2021/3/28
目次
一般的なプロパティ
public string MyProp {get; set;}
メモ:この構文を自動実装プロパティと呼びます。
初期値も設定できます。
public string MyProp {get; set;} = "初期値";
メモ:System.ComponentModel.DefaultValue 属性 も参考。
読み取り専用プロパティ
public string MyProp {get;} = "初期値";
メモ:読み取り専用の自動実装プロパティに値を設定するには、初期値を記述するか、コンストラクターを使用します。init専用プロパティ(init専用セッター)の採用も検討します。
C# 2.0(2005年)以前の場合、次のようにします。
private string myPropValue = "初期値";
public string MyProp
{
get
{
return myPropValue;
}
}
初期化時のみ値を設定できるプロパティ
public class Test
{
public string MyProp {get; init;} = "初期値";
}
メモ:この例のように init が定義されている場合、初期化時またはインスタンスのコンストラクターまたはinitアクセサー(後述)でのみ値を設定できます。
使用例
Test t = new Test() {MyProp = "ABC"};
// t.MyProp = "XYZ"; //←これはエラーにあります。MyPropに値を設定できるのは初期化時のみです。
initアクセサーで値を設定する例。C# 7.0(2017年)以上の場合。
private string myPropValue = "初期値";
public string MyProp
{
get => myPropValue;
init => myPropValue = "あいうえお";
}
C# 7.0より前の場合は次のようにします。
private string myPropValue = "初期値";
public string MyProp
{
get
{
return myPropValue;
}
init
{
myPropValue = "あいうえお";
}
}
処理を実行するプロパティ
この FilePathプロパティには実在しないパスを設定しようとすると例外を発生させます。
FilePathプロパティが設定されていないか、設定後削除されたなどの場合は、マイドキュメントのパスを表します。
private string filePathValue;
public string FilePath
{
get
{
if (!System.IO.Directory.Exists(filePathValue))
{
//パスが存在しない場合 マイドキュメントを使用します。.NET Framework 1.1(2003年)以前の場合 SpecialFolder.Personal を使用してください。
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
return filePathValue;
}
set
{
if (System.IO.Directory.Exists(value))
{
filePathValue = value;
}
else
{
throw new InvalidOperationException("存在しないパスは指定できません。");
}
}
}
値の設定と読み取りでアクセスレベルが異なるプロパティ
次のプロパティは値の取得は Public で、値の設定は Protected です。
public string MyProp {get; protected set;}
これでもできます。
private string myPropValue = "初期値";
public string MyProp
{
get
{
return myPropValue;
}
protected set
{
myPropValue = value;
}
}
式形式のプロパティ
private string myPropValue = "初期値";
public string MyProp
{
get => myPropValue;
set => myPropValue = value??"";
}
メモ:C# 7.0(2017年)以上の場合のみ使用できます。
引数付きのプロパティ
C#では引数付きのプロパティを定義することはできません。VBでは通常の構文で定義できます。
※2021年3月時点。今後C#にこの機能が追加されるという情報もありません。
参考:インデクサー(≒引数付きのプロパティ)
public class UnicodeString
{
public string Text { get; set; }
public UnicodeString(string text)
{
this.Text = text;
}
/// <summary>
/// 指定された位置にある文字を取得します。
/// </summary>
public string this[int index]
{
get
{
var itor = System.Globalization.StringInfo.GetTextElementEnumerator(this.Text);
do
{
itor.MoveNext();
} while (itor.ElementIndex < index);
return itor.Current.ToString();
}
}
}
メモ:この例ではインデクサーは get のみで構成されているので読み取り専用です。set を定義して読み書きできるようにすることも可能です。
呼び出し例
var unicode = new UnicodeString("明日は🗻Let's Go!");
string c1 = unicode[0]; //明
string c2 = unicode[1]; //日
string c3 = unicode[2]; //は
string c4 = unicode[3]; //🗻
string c5 = unicode[4]; //L
C# 2.0(2005年)以前で一般的だったプロパティ定義方法
C# 2.0以前は自動実装プロパティが使用できないため、単純に値を保持するプロパティでも次のように書く必要があります。
private string myPropValue = "初期値";
public string MyProp
{
get
{
return myPropValue;
}
set
{
myPropValue = value;
}
}