在另一个 class c# 中调用 var 用户

Call var users in another class c#

我有一个名为 var users=JsonConvert.DeserializedObject<User[]>(jsonResponse) 的变量,它位于 public class login 内的 Login.cs

所以我想做的是在 Menu.cs 中调用 var users 以便能够在 var users 中编辑相同的数据但是我不能声明 var users as public var users 因为这是不允许的。这就是为什么我试图为此寻找另一种方法或解决方法。

public class Login 
{
    public static string jsonResponse = File.ReadAllText("C:\Users\Admin\C#_Activity 1\Users.json");

    public static void LoginMenu()
    {
        var users = JsonConvert.DeserializeObject<User[]>(jsonResponse);
    }

}

public class Menu
{
     
    public static void BankMenu()
    {
        foreach (var user in users!)
        {
            Console.Writeline(user.FirstName); 
        }
    }
}
Users.json
[
  {
    "id": 1,
    "username": "sachin",
    "password": "sachin1",
    "firstName": "Sachin",
    "lastName": "Karnik",
    "birthdate": "2011-6-14",
    "balance": 20000,
    "cardNumber": 12345
  },
  {
    "id": 2,
    "username": "dina",
    "password": "dina1",
    "firstName": "Dina",
    "lastName": "Meyers",
    "birthdate": "2012-8-20",
    "balance": 20000,
    "cardNumber": 23456
  },
  {
    "id": 3,
    "username": "andy",
    "password": "andy1",
    "firstName": "Andy",
    "lastName": "Rose",
    "birthdate": "2010-2-11",
    "balance": 20000,
    "cardNumber": 34567
  }
]

使 users 成为 public 静态成员而不是 jsonResponse。据我所知,没有理由让 jsonReponse 成为 public 成员。像这样:

public class Login 
{
    public static User[] Users { get; private set;}

    public static void LoginMenu()
    {
        var json = File.ReadAllText("C:\Users\Admin\C#_Activity 1\Users.json");
        Users = JsonConvert.DeserializeObject<User[]>(json);
    }
}