C#中的字符串类型列表

时间:2011-11-11 07:22:02

标签: c#

我有一个清单

    List<string> ParamValues = new List<string>();
    ParamValues.Add(txtEmployeeId.Text);
    ParamValues.Add(txtPassword.Text)

在Login.aspx.cs文件中。

我想将此列表用作方法的参数。该方法位于DataAccess.cs类文件中。 我该怎么办?

5 个答案:

答案 0 :(得分:1)

如果方法是

public void MyMethods(System.Collections.Generic.List<string> paramValue)
{
 //Write your code here
}

称之为

DataAcess da= new DataAcess();
 da.MyMethods(ParamValue)

在您的网页

答案 1 :(得分:1)

假设该方法被声明为接受一个List<string>(或IList<string>等),您只需使用ParamValues变量作为参数调用该方法:

调用静态方法:

DataAccess.SomeMethod(ParamValues);

调用实例方法:

DataAccess data = new DataAccess();
data.SomeMethod(ParamValues);

(顺便说一句,我会重命名变量 - 在PascalCase中看到C#变量是不常见的。)

现在如果你实际得到这样的东西:

public void SomeMethod(string id, string password)

并且你想要为每个参数调用填充一个参数的方法,然后你需要使用反射来做到这一点。

答案 2 :(得分:0)

您将此列表作为参数传递,就像使用普通变量一样。

答案 3 :(得分:0)

创建字符串类型列表

List<string> paramValues = new List<string>();

将字符串添加到列表并传递给DBAccess方法。

答案 4 :(得分:0)

让DataAccess.cs文件中的方法将列表作为参数。
public void MyMethod(List list) { }