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

时间:2019-01-30 16:19:26

标签: c# visual-studio

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

我有一个名为Player的公共类和一个名为playerNamepublic的字符串变量static。在我的Program.cs中,我尝试使用Player.playerName = Console.ReadLine();分配变量,但是稍后调用它时,它什么也不返回。

1 个答案:

答案 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