我试图将字符串分配给单独类中的变量,然后稍后在代码中调用该变量。如何写其他类中的变量并在以后使用它们?
我有一个名为Player
的公共类和一个名为playerName
和public
的字符串变量static
。在我的Program.cs中,我尝试使用Player.playerName = Console.ReadLine();
分配变量,但是稍后调用它时,它什么也不返回。
答案 0 :(得分:4)
您能否提供代码示例或更好地描述您的问题?
一个通用的解决方案可以是:
class Example
{
public string MyProperty { get; set; }
}
然后:
//instance of the class
Example example = new Example();
//set the property:
example.MyProperty = "value";
//access the property:
example.MyProperty