What is C# analog of C++ std::pair?

前端 未结 14 1929
情话喂你
情话喂你 2020-11-29 16:54

I\'m interested: What is C#\'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I\'d prefer something template-based.

Than

14条回答
  •  有刺的猬
    2020-11-29 17:23

    Apart from custom class or .Net 4.0 Tuples, since C# 7.0 there is a new feature called ValueTuple, which is a struct that can be used in this case. Instead of writing:

    Tuple t = new Tuple("Hello", 4);
    

    and access values through t.Item1 and t.Item2, you can simply do it like that:

    (string message, int count) = ("Hello", 4);
    

    or even:

    (var message, var count) = ("Hello", 4);
    

提交回复
热议问题