2012-03-20 26 views
6

Nella mia pagina di caricamento, sto chiamando ReturnStuff() una o tre volte? Se lo chiamo tre volte, c'è un modo più efficiente per farlo?Sto usando gli elenchi correttamente?

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

Stai chiedendo se stai chiamando il metodo tre volte o no? –

+1

Sì, questo e come farlo in modo più efficiente. – mmcglynn

risposta

13

Lo stai chiamando tre volte. Ecco un modo più efficiente:

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

Ma più di questo, se si dispone di un nome, il cognome e e-mail, vorrei scrivere una funzione che restituisce un oggetto la composizione di un nome, il cognome e e-mail:

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, questo è molto per avvolgere la mia testa n00b in giro, quindi grazie! – mmcglynn

+0

@mmcglynn: puoi concentrarti sulla parte relativa alla restituzione dell'elenco una volta prima, quindi :) – BoltClock

1

Lo stai chiamando 3 volte. Chiamalo una volta e salva i risultati in una variabile, quindi puoi lavorare con quella variabile.

Prova questo:

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

Perché stai usando la variante? qualche significato qui? perché non i tipi forti? – Pankaj

+2

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

+0

Uso var per ridurre il rapporto segnale/rumore nel mio codice. Non è sempre appropriato, e nemmeno sempre una buona idea. Leggi questo. http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp – squillman

1

3 volte. seguente codice ti aiuterà a realizzare. vai alla funzione principale e chiama func() da lì.

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

Si dovrebbe chiamare quella funzione una volta, ottenere il valore restituito in una lista locale <> variabile e quindi accedere utilizzando la variabile. In questo modo:

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