このクラスを考えてみましょう:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game.Items
{
class Item
{
private string name;
public string Name
{
get { return this.name; }
}
private string description;
public string Description
{
get { return this.description; }
}
public Item(string name, string description)
{
this.name = name;
this.description = description;
}
public override string ToString()
{
return this.name;
}
}
}
次に、次のような新しいオブジェクトを作成します。
Item item1 = new Item("Item1", "Description...");
問題は、getter メソッドを使用してオブジェクトのプロパティにアクセスできないことです。つまり、これは機能しません。
Console.WriteLine(item1.Name);
Console.WriteLine(item1.Description);
Console.ReadLine();
「動作しない」とは、「デバッグの開始」をクリックすると、コンソールウィンドウが表示されますが、何も表示されず、空白です。ただし、これは正常に機能します。
Console.WriteLine(item1); // ToString()
Console.ReadLine();
私は何を間違っていますか?
リチャード