C# 将字符串添加到字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15232813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add strings to string array
提问by Rehan Manzoor
class Myclass
{
static string[] user_Name = { "admin", "user1", "user2" };
static string[] user_Password ={ "admin", "123", "789" };
public static void Check_Method(string u_name, string u_password)
{
for (int i = 0; i < user_Name.Length; i++)
{
if (u_name == user_Name[i] && u_password == user_Password[i])
{
MessageBox.Show("login successful");
break;
}
else
{
if (i == (user_Name.Length - 1))
MessageBox.Show("Badshow");
}
}
}
public static void add_user(string name, string password)
{
i=user_Name.Length;
user_Name[i]=name;
user_Password[i]=password;
//here i want to add another user but im unable to find the way
}
}
But it gives an error that it is outside the boundary of an array.
但是它给出了一个错误,它在数组的边界之外。
What could be the most convenient way to perform this action?
执行此操作的最便捷方法是什么?
回答by Oded
Don't use arrays if you need variable sized storage.
如果您需要可变大小的存储,请不要使用数组。
Use List<string>
instead - it allows you to Add
items.
使用List<string>
代替-它可以让你Add
的项目。
In your case, your choice of two arrays is questionable, as each user has a corresponding password - always. This suggests that you should have a custom class to hold a user/password pair.
在您的情况下,您对两个数组的选择是有问题的,因为每个用户都有一个相应的密码 - 总是如此。这表明您应该有一个自定义类来保存用户/密码对。
With such a class (say User
), you would hold a List<User>
and simplify your code.
使用这样的类(例如User
),您将拥有一个List<User>
并简化您的代码。
回答by Dustin Kingen
Try using a List<>
.
尝试使用List<>
.
class Myclass
{
static List<string> user_Name = new List<string>{ "admin", "user1", "user2" };
static List<string> user_Password = new List<string>{ "admin", "123", "789" };
public static void Check_Method(string u_name, string u_password)
{
for (int i = 0; i < user_Name.Length; i++)
{
if (u_name == user_Name[i] && u_password == user_Password[i])
{
MessageBox.Show("login successful");
break;
}
else
{
if (i == (user_Name.Length - 1))
MessageBox.Show("Badshow");
}
}
}
public static void add_user(string name, string password)
{
user_Name.Add(name);
user_Password.Add(password);
}
}
Here's a refactored version:
这是一个重构版本:
Users are contained in a user class.
用户包含在用户类中。
They are IEquatable<>
which compares their username/passwords (you might want to consider looking into a Guid
to keep them unique).
它们是IEquatable<>
比较他们的用户名/密码(您可能需要考虑查看 aGuid
以保持它们的唯一性)。
Easily add/remove users.
轻松添加/删除用户。
public class User : IEquatable<User>
{
public User(string name, string password)
{
Name = name;
Password = password;
}
public string Name { get; set; }
public string Password { get; set; }
public bool Equals(User other)
{
if (other == null) return false;
return other.Name == Name && other.Password == Password;
}
}
public class AuthenticationManager
{
private List<User> LoggedInUsers = new List<User>
{ new User("Admin", "admin"), new User ("user1", "123"), new User ("user2", "789") };
public bool Authenticate(string userName, string password)
{
var user = new User(userName, password);
//if the user is in the list it will return false otherwise true.
return !LoggedInUsers.Any(u => user.Equals(user));
}
public void Login(string name, string password)
{
LoggedInUsers.Add(new User(name, password));
}
public void Logout(string name, string password)
{
LoggedInUsers.Remove(new User(name, password));
}
}
回答by Mark Walsh
Okay I think you might be thinking about this the wrong way. The way in which you're using arrays is screaming out for an object.
好吧,我想你可能想错了。您使用数组的方式是为一个对象而尖叫。
I would made User an object like so
我会让 User 像这样一个对象
public class User
{
public string UserName { get; set;}
public string Password { get; set;}
}
I would then maintain a list of Users instead. That way you won't need to maintain array indexes and you can easily add new users into the list.
然后我会维护一个用户列表。这样您就不需要维护数组索引,并且可以轻松地将新用户添加到列表中。
回答by Felipe Oriani
Why don't you use and List
and apply a DTO instead of multiples string[]
?
为什么不使用 andList
和应用 DTO 而不是 multiples string[]
?
Try something like this:
尝试这样的事情:
1) Create a DTO for your Users:
1) 为您的用户创建一个 DTO:
public class UserDTO
{
public string UserName { get; set; }
public string Password { get; set; }
}
2) Use and List<DTO>
2) 使用和 List<DTO>
class Myclass
{
static List<UserDTO> users = new List<UserDTO>()
{
new UserDTO() { UserName= "admin", Password = "admin" } ,
new UserDTO() { UserName= "user1", Password = "123" } ,
new UserDTO() { UserName= "user2", Password = "789" } ,
}
public static void Check_Method(string u_name, string u_password)
{
if (users.Exists(x => x.UserName == u_name && x.Password == u_password)
{
MessageBox.Show("login successful");
}
else
{
MessageBox.Show("Badshow");
}
}
public static void add_user(string name, string password)
{
users.Add(new UserDTO() { UserName= name, Password = password });
}
}
回答by Ahmed AlShahawi
Try use List<string>
class instead of string[]
尝试使用List<string>
类而不是string[]
and add items to array using object.Add()
method
并使用object.Add()
方法将项目添加到数组