如何在 C# 中的单独类中分配/编辑变量



我正在尝试将字符串分配给单独类中的变量,然后稍后在代码中调用该变量。如何写入其他类中的变量并在以后使用它们?

我有一个名为 Player 的公共类和一个名为 playerName 的字符串变量,声明为 publicstatic .在我的程序中.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

最新更新