SORU
3 EKİM 2008, Cuma


C# C analog std::pair?

C ne olduğunu merak ediyorum# C analog std::pair? Sistem buldum.Web.UI.Sınıf çifti, ama bir şey dayalı şablon istedim.

Teşekkür ederim!

CEVAP
3 EKİM 2008, Cuma


Dizilerini will be a part of .NET4.0 ve jenerik destekler:

Tuple<string, int> t = new Tuple<string, int>("Hello", 4);

System.Collections.Generic.KeyValuePair<K, V> veya aşağıdaki gibi bir çözüm kullanabilirsiniz:

public class Pair<T, U> {
    public Pair() {
    }

    public Pair(T first, U second) {
        this.First = first;
        this.Second = second;
    }

    public T First { get; set; }
    public U Second { get; set; }
};

Ve aşağıdaki gibi kullanabilirsiniz:

Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);

Bu çıkışları:

test
2

Hatta bu zincirleme çift:

Pair<Pair<String, int>, bool> pair = new Pair<Pair<String, int>, bool>();
pair.First = new Pair<String, int>();
pair.First.First = "test";
pair.First.Second = 12;
pair.Second = true;

Console.WriteLine(pair.First.First);
Console.WriteLine(pair.First.Second);
Console.WriteLine(pair.Second);

Bu çıkışları:

test
12
true

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BruBearBaby

    BruBearBaby

    25 Ocak 2011
  • David MeShow

    David MeShow

    10 EKİM 2006
  • Sergio Fernandez

    Sergio Ferna

    1 EKİM 2009

İLGİLİ SORU / CEVAPLAR