2012-03-20 11 views
6

En mi carga de página, ¿llamo al ReturnStuff() una o tres veces? Si lo llamo tres veces, ¿hay alguna manera más eficiente de hacerlo?¿Estoy usando listas correctamente?

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; 
} 
+1

¿Estás preguntando si estás llamando al método tres veces o no? –

+1

Sí, eso y cómo hacer esto de manera más eficiente. – mmcglynn

Respuesta

13

Lo llama tres veces. Aquí está una manera más eficiente:

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]; 
} 

Pero más que eso, si usted tiene un primer nombre, apellido y e-mail, me gustaría escribir una función que devuelve un objeto que compone un primer nombre, apellido y correo electrónico:

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); 
} 
+0

Dang it, sniped ... – squillman

+0

Wow, eso es mucho para envolver mi cabeza n00b, ¡así que gracias! – mmcglynn

+0

@mmcglynn: Puede concentrarse en la parte de devolver la lista una vez primero, luego :) – BoltClock

1

Lo está llamando 3 veces. Llámalo una vez y guarda los resultados en una variable, luego puedes trabajar con esa variable.

Prueba esto:

var stuff = ReturnStuff(username,password); 
string thing1 = stuff[0]; 
string thing2 = stuff[1]; 
string thing3 = stuff[2]; 
+0

¿Por qué está usando la variante? cualquier significado aquí? ¿Por qué no tipos fuertes? – Pankaj

+2

@Pankaj Garg: Erm, "variante"? – BoltClock

+0

Uso var para reducir la relación señal/ruido en mi código. No siempre es apropiado, y ni siquiera es una buena idea. Lee esto. http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp – squillman

1

3 veces. siguiente código le ayudará a darse cuenta. ve a la función Principal y llama a func() desde allí.

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); 
     } 
    } 

Usted debe llamar a esa función una vez, obtener el valor devuelto en una lista local <> variable y luego acceder a través de la variable. como este:

List<string> list = function-that-returns-List<string>(); 
list[0]; //Do whatever with list now. 
Cuestiones relacionadas