C# 我是否正确使用列表?

C# 我是否正确使用列表?,c#,asp.net,list,C#,Asp.net,List,在页面加载中,我是调用ReturnStuff()一次还是三次? 如果我打三次电话,有没有更有效的方法 protected void Page_Load(object sender, EventArgs e) { string thing1 = ReturnStuff(username,password)[0]; string thing2 = ReturnStuff(username, password)[1]; string thing3 = ReturnStuff(u

在页面加载中,我是调用
ReturnStuff()
一次还是三次? 如果我打三次电话,有没有更有效的方法

protected void Page_Load(object sender, EventArgs e)
{
    string thing1 = ReturnStuff(username,password)[0];
    string thing2 = ReturnStuff(username, password)[1];
    string thing3 = ReturnStuff(username, password)[2];
}

public static List<string> ReturnStuff(string foo, string bar)
{

    // Create a list to contain the attributes
    List<string> Stuff = new List<string>();

    // Some process that determines strings values based on supplied parameters

    Stuff.Add(fn);
    Stuff.Add(ln);
    Stuff.Add(em);

    return Stuff;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
string thing1=ReturnStuff(用户名、密码)[0];
string thing2=ReturnStuff(用户名、密码)[1];
string thing3=ReturnStuff(用户名、密码)[2];
}
公共静态列表ReturnStuff(字符串foo、字符串栏)
{
//创建一个包含属性的列表
列表内容=新列表();
//某些进程根据提供的参数确定字符串值
添加(fn);
添加(ln);
填充。添加(em);
归还物品;
}

你已经叫了三次了。以下是一种更有效的方法:

protected void Page_Load(object sender, EventArgs e)
{
    var stuff = ReturnStuff(username,password);
    string thing1 = stuff[0];
    string thing2 = stuff[1];
    string thing3 = stuff[2];
}
但除此之外,如果您有名字、姓氏和电子邮件,我将编写一个函数,返回由名字、姓氏和电子邮件组成的对象:

public class User
{
     public string LastName {get;set;}
     public string FirstName {get;set;}
     public string EMail {get;set;}
}

public static User GetUser(string username, string password)
{ 
    // Some process that determines strings values based on supplied parameters

    return new User() {FirstName=fn, LastName=ln, EMail=em};
}

protected void Page_Load(object sender, EventArgs e)
{
    var user = GetUser(username,password);
}

你打了三次电话。调用它一次并将结果保存到变量中,然后就可以使用该变量了

试试这个:

var stuff = ReturnStuff(username,password);
string thing1 = stuff[0];
string thing2 = stuff[1];
string thing3 = stuff[2];
三次。 下面的代码将帮助您实现。转到Main函数并从那里调用func()

class howmanytimescallingafunction
     {
        public static int i = 0;
        public List<string> fun()
        {
            List<string> list = new List<string> { "A", "B", "C" };
            i++;
            return list;
        }
        public void func()
        {
            Console.WriteLine(fun()[0]);
            Console.WriteLine(i);
            Console.WriteLine(fun()[1]);
            Console.WriteLine(i);
            Console.WriteLine(fun()[2]);
            Console.WriteLine(i);
        }
    }
类howmanyTimeCallinga函数
{
公共静态int i=0;
公开名单(只提供英文版本)
{
列表=新列表{“A”、“B”、“C”};
i++;
退货清单;
}
公共无效函数()
{
Console.WriteLine(fun()[0]);
控制台写入线(i);
Console.WriteLine(fun()[1]);
控制台写入线(i);
Console.WriteLine(fun()[2]);
控制台写入线(i);
}
}
您应该调用该函数一次,获取本地列表变量中的返回值,然后使用该变量进行访问。像这样:

List<string> list = function-that-returns-List<string>();
list[0]; //Do whatever with list now.
List List=function-that-returns-List();
列表[0]//现在就用列表做任何事情。

您是否在询问是否要调用该方法三次?是的,以及如何更有效地执行此操作。为什么要使用variant?这里有什么意义吗?为什么不是强类型?@Pankaj-Garg:Erm,“variant”?我使用var来降低代码中的信噪比。这并不总是合适的,甚至不总是一个好主意。通读一下这个。为什么您不喜欢动态?我使用var而不是dynamic来维护静态类型。我宁愿让编译器来处理我的错误,而不是运行时。哇,这对我的n00b来说太难了,所以谢谢你@mmcglynn:你可以先集中精力返回列表一次,然后:)@BoltClock。知道了。这是一个非常好的简单类示例,这对我很有帮助。