我试图将一个字符串分配给一个单独的类中的变量,然后稍后在代码中调用该变量。如何写入其他类中的变量并在以后使用它们?
我有一个名为 Player
的公共(public)类(class)和一个名为 playerName
的字符串变量声明为public
和static
。在我的 Program.cs 中,我尝试使用 Player.playerName = Console.ReadLine();
分配变量但是,当我稍后调用它时,它什么也没有返回。
最佳答案
您能否提供代码示例或更好地描述您的问题?
一种通用解决方案可以是:
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
关于c# - 在 C# 中,如何在单独的类中分配/编辑变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54444989/