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;
}
}
orTuple
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?
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);
}
}If you don't require a sorted set, use
HashSet<T>
.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()
orTask.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
When Not to Use Yield (Return)
Best Way to Share Data Between Two Child Components in Blazor
Ensure That Httpconfiguration.Ensureinitialized()
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
How to Create Byte Array from Httppostedfile
Dynamically Updating Tabcontrol Content at Runtime
Why Are Tolookup and Groupby Different
Get All Properties Which Marked Certain Attribute
Can Connect to Ftp Using Filezilla or Winscp, But Not with Ftpwebrequest or Fluentftp
Why Can't I Use Interface with Explicit Operator
How to Do Tostring for a Possibly Null Object
Write Device Platform Specific Code in Xamarin.Forms
Unit Testing That Events Are Raised in C# (In Order)
How to Apply a General Rule for Remapping All Property Names When Serializing with JSON.Net
Setting Up Swagger (ASP.NET Core) Using the Authorization Headers (Bearer)