私は興味があります:std::pair
C ++でのC#のアナログは何ですか?クラスを見つけましSystem.Web.UI.Pair
たが、テンプレートベースのものがいいと思います。
ありがとうございました!
私は興味があります:std::pair
C ++でのC#のアナログは何ですか?クラスを見つけましSystem.Web.UI.Pair
たが、テンプレートベースのものがいいと思います。
ありがとうございました!
タプルは.NET4.0以降で使用可能であり、ジェネリックをサポートしています。
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
以前のバージョンでは、System.Collections.Generic.KeyValuePair<K, V>
または次のようなソリューションを使用できます。
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; }
};
そして、次のように使用します。
Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);
これは以下を出力します:
test
2
または、この連鎖ペアですら:
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);
その出力:
test
12
true
System.Web.UI
Pair
ASP.NET 1.1で内部ViewState構造として頻繁に使用されていたため、このクラスが含まれていました。
2017年8月の更新: C#7.0 / .NET Framework 4.7は、System.ValueTuple
構造体を使用して名前付きアイテムでタプルを宣言する構文を提供します。
//explicit Item typing
(string Message, int SomeNumber) t = ("Hello", 4);
//or using implicit typing
var t = (Message:"Hello", SomeNumber:4);
Console.WriteLine("{0} {1}", t.Message, t.SomeNumber);
その他の構文例については、 MSDNを参照してください。
2012年6月の更新: Tuples
バージョン4.0以降.NETの一部になっています。
これは、.NET4.0への組み込みとジェネリックスのサポートについて説明した以前の記事です。
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
残念ながら、ありません。System.Collections.Generic.KeyValuePair<K, V>
多くの場面で使用できます。
または、匿名型を使用して、少なくともローカルでタプルを処理できます。
var x = new { First = "x", Second = 42 };
最後の選択肢は、独自のクラスを作成することです。
C#には、バージョン4.0の時点でタプルがあります。
いくつかの答えは間違っているように見えますが、
ペアクラスはこちら
public class Pair<X, Y>
{
private X _x;
private Y _y;
public Pair(X first, Y second)
{
_x = first;
_y = second;
}
public X first { get { return _x; } }
public Y second { get { return _y; } }
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj == this)
return true;
Pair<X, Y> other = obj as Pair<X, Y>;
if (other == null)
return false;
return
(((first == null) && (other.first == null))
|| ((first != null) && first.Equals(other.first)))
&&
(((second == null) && (other.second == null))
|| ((second != null) && second.Equals(other.second)));
}
public override int GetHashCode()
{
int hashcode = 0;
if (first != null)
hashcode += first.GetHashCode();
if (second != null)
hashcode += second.GetHashCode();
return hashcode;
}
}
ここにいくつかのテストコードがあります:
[TestClass]
public class PairTest
{
[TestMethod]
public void pairTest()
{
string s = "abc";
Pair<int, string> foo = new Pair<int, string>(10, s);
Pair<int, string> bar = new Pair<int, string>(10, s);
Pair<int, string> qux = new Pair<int, string>(20, s);
Pair<int, int> aaa = new Pair<int, int>(10, 20);
Assert.IsTrue(10 == foo.first);
Assert.AreEqual(s, foo.second);
Assert.AreEqual(foo, bar);
Assert.IsTrue(foo.GetHashCode() == bar.GetHashCode());
Assert.IsFalse(foo.Equals(qux));
Assert.IsFalse(foo.Equals(null));
Assert.IsFalse(foo.Equals(aaa));
Pair<string, string> s1 = new Pair<string, string>("a", "b");
Pair<string, string> s2 = new Pair<string, string>(null, "b");
Pair<string, string> s3 = new Pair<string, string>("a", null);
Pair<string, string> s4 = new Pair<string, string>(null, null);
Assert.IsFalse(s1.Equals(s2));
Assert.IsFalse(s1.Equals(s3));
Assert.IsFalse(s1.Equals(s4));
Assert.IsFalse(s2.Equals(s1));
Assert.IsFalse(s3.Equals(s1));
Assert.IsFalse(s2.Equals(s3));
Assert.IsFalse(s4.Equals(s1));
Assert.IsFalse(s1.Equals(s4));
}
}
辞書などの場合は、System.Collections.Generic.KeyValuePair <TKey、TValue>を探しています。
私は Tuples の C# 実装を作成しました。これは、2 ~ 5 つの値の間で問題を一般的に解決します。ソースへのリンクを含むブログ投稿はこちらです。
簡単なグーグルの直後に同じ質問をしていましたが、System.Web.UI以外に.NETにペアクラスがあることがわかりました^〜^(http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx)良さは、コレクションフレームワークの代わりにそこに配置する理由を知っています
達成したい内容によっては、KeyValuePairを試してみることをお勧めします。
エントリのキーを変更できないという事実は、もちろんエントリ全体を KeyValuePair の新しいインスタンスで置き換えるだけで修正できます。
通常Tuple
、次のようにクラスを独自の汎用ラッパーに拡張します。
public class Statistic<T> : Tuple<string, T>
{
public Statistic(string name, T value) : base(name, value) { }
public string Name { get { return this.Item1; } }
public T Value { get { return this.Item2; } }
}
次のように使用します。
public class StatSummary{
public Statistic<double> NetProfit { get; set; }
public Statistic<int> NumberOfTrades { get; set; }
public StatSummary(double totalNetProfit, int numberOfTrades)
{
this.TotalNetProfit = new Statistic<double>("Total Net Profit", totalNetProfit);
this.NumberOfTrades = new Statistic<int>("Number of Trades", numberOfTrades);
}
}
StatSummary summary = new StatSummary(750.50, 30);
Console.WriteLine("Name: " + summary.NetProfit.Name + " Value: " + summary.NetProfit.Value);
Console.WriteLine("Name: " + summary.NumberOfTrades.Value + " Value: " + summary.NumberOfTrades.Value);
PowerCollections ライブラリ (以前は Wintellect から入手できましたが、現在は Codeplex @ http://powercollections.codeplex.comでホストされています) には、一般的なペア構造があります。
上記を機能させるために(辞書のキーとしてペアが必要でした)。追加する必要がありました:
public override Boolean Equals(Object o)
{
Pair<T, U> that = o as Pair<T, U>;
if (that == null)
return false;
else
return this.First.Equals(that.First) && this.Second.Equals(that.Second);
}
そして、私がそれをしたら、私も追加しました
public override Int32 GetHashCode()
{
return First.GetHashCode() ^ Second.GetHashCode();
}
コンパイラの警告を抑制します。