What Is C# Analog of C++ Std::Pair

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

Tuples are available since .NET4.0 and support generics:

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

In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following:

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

And use it like this:

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

This outputs:

test
2

Or even this chained pairs:

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

That outputs:

test
12
true

How to translate make_pair in c++ to c#?

You are declaring a dictionary to use KeyValuePair<K,V> keys, but you are trying to access it with Tuple<T1,T2> instead. You need to decide on one type, and stay with it.

Since KeyValuePair<K,V> is asymmetric, I would use Tuple<T1,T2>:

private Dictionary<Tuple<uint, uint>, uint> kmapValues;

Then your assignment would work correctly:

kmapValues[Tuple.Create(j, i)] = 1;

What is equivalent to DictionaryTKey, TValue with size one (count) 1 in C#?

You're looking for a KeyValuePair<TKey, TValue>:

var keyValuePair = new KeyValuePair<int, int>(1, 1);

Is there an analogue of keyword where from C# in C++?

C++20 offers you the closest solution to C#:

#include <concepts>

template <class T>
concept MyInterface = requires(T x)
{
{ x.GetSomeInteger() } -> std::same_as<int>;
};

And then:

template <MyInterface T>
struct MyClass
{
// ...
};

How can I return more than one object of different type in C# .net?

public class VPair<TFirst, TSecond>
{
public TFirst First { get; set; }
public TSecond Second { get; set; }

public VPair(TFirst first, TSecond second)
{
First = first;
Second = second;
}
}

or
Tuple class in c#4.0

bonus:

public class VTriple<TFirst, TSecond, TThird> : VPair<TFirst, TSecond>
{
public TThird Third { get; set; }

public VTriple(TFirst first, TSecond second, TThird third)
: base(first, second)
{
Third = third;
}
}

What is the C# equivalent of the stl set?

  1. If you require sorted set, use SortedDictionary<T,U>. This is implemented using a binary search tree. Admittedly, you will be using 64-bits per entry because you are storing a key-value pair underneath. You can write a wrapper around it like this:

    class Set<T> : SortedDictionary<T, bool>
    {
    public void Add(T item)
    {
    this.Add(item, true);
    }
    }
  2. If you don't require a sorted set, use HashSet<T>.

  3. Otherwise, check out C5 Generic Collection Library. In particular TreeSet<T>. It is a red-black tree and only stores the values.

C# equivalent of C++ deferred async execution?

If my quick google search was correct then launch::deferred means lazy evaluation on the calling thread.

In that case, using a task might not be a good idea, they are not meant for lazy evaluation because:

  • Awaiting them or taking their result does not start them if they were
    not started already (and starting them while they are already running throws an exception)
  • You can't run them on the calling thread, they represent an asynchronous operation (such as IO) or work running on a ThreadPool thread
  • Even if you could run them on the calling thread if they were not started yet, they are typically created using Task.Run() or Task.Factory.StartNew() which starts them right away

Perhaps you could use System.Lazy<T> instead. It seems to do what you need, including several thread safety options.

C# equivalent of C++ mapstring,double

Roughly:-

var accounts = new Dictionary<string, double>();

// Initialise to zero...

accounts["Fred"] = 0;
accounts["George"] = 0;
accounts["Fred"] = 0;

// Add cash.
accounts["Fred"] += 4.56;
accounts["George"] += 1.00;
accounts["Fred"] += 1.00;

Console.WriteLine("Fred owes me ${0}", accounts["Fred"]);


Related Topics



Leave a reply



Submit