C#如何存储存储在循环内部或外部变量的变量
我想将for loop或foreach内部的变量存储到函数外部的变量中C#如何存储存储在循环内部或外部变量的变量,c#,C#,我想将for loop或foreach内部的变量存储到函数外部的变量中 Class User{ public int password; public int[] user_password; public static int[] Password = new int[40]; int counter = 0; public void register(){ Console.WriteLine("Enter member's password
Class User{
public int password;
public int[] user_password;
public static int[] Password = new int[40];
int counter = 0;
public void register(){
Console.WriteLine("Enter member's password?");
int password = int.Parse(Console.ReadLine());
Password[counter] = password;
counter++;
for(int i = 0; i < counter; i++){
user_password[i] = Password[i];
}
}
public int getpassword(){
return user_password[];
}
}
类用户{
公共int密码;
public int[]用户密码;
公共静态int[]密码=新int[40];
int计数器=0;
公开作废登记册(){
Console.WriteLine(“输入成员密码?”);
int password=int.Parse(Console.ReadLine());
密码[计数器]=密码;
计数器++;
对于(int i=0;i
我想在这里做的是,我想把passwordraray的所有值存储到一个名为user\u password
的新数组中,然后在另一个类中调用getpassword函数。
但是,上面的代码没有在user\u password
数组中存储任何内容,它只返回null
我应该如何处理这个问题?我需要考虑使用GET SET吗?< /P>
谢谢。
不考虑这里的安全问题,只是保存这样的密码,但是考虑使用字典。类似这样的事情(你必须适应你的现实):
要获取现有的文件,请执行以下操作:return userPsw["my_user"];
甚至:
userPsw.TryGetValue("my_user", out value);
我不明白你想要实现什么。问题是否在
寄存器中
或获取密码中
?是user_password[]中缺少的索引代码>输入错误?你是说用户密码[计数器]
或public getpassword(int-index){return user_password[index];}
这里的密码是int数组吗?这正常吗?用户
是否有多个密码,或者这个类代表的是一个拥有一个属性的用户,该属性存储所有其他用户的密码?坦率地说,我想使用getpassword()返回所有密码。但是上面的代码没有返回任何值,您的代码有很多问题,我不知道从哪里开始。也许你可以解释一下你想用这个代码做什么?在解释时,不要参考你的代码,只是你试图解决的需求。我忘了提到,我不能真正使用数组以外的任何东西。
return userPsw["my_user"];
userPsw.TryGetValue("my_user", out value);