hazelcast / hazelcast/hazelcast-csharp-client
Add a bool TryGet(TKey key, out TValue value) method to IMap [API-2049]
- Dominant language
- C#
- Stars
- 107
- Forks
- 52
- PR merge metrics
- No merged PRs in 30d
Description
Right now when the value of a map is a primitive (not-nullable) type, the following code must be used to verify if there's a value for a given key and subsequently get the value:
```cs
double GetTaxRate(IHazelcastInstance instance, int zipCode)
{
var taxRates = instance.GetMap("TaxRates");
if (!taxRates.ContainsKey(zipCode))
{
throw new ArgumentException($"No tax rate defined for zip code '{zipCode}'.", nameof(zipCode));
}
return taxRates.Get(zipCode);
}
```
I propose to introduce a `bool TryGet(TKey key, out TValue value)` method on **IMap**.
This would:
* Save a roundtrip to the server.
* Eliminate a concurrency issue between the `ContainsKey(...)` and `Get(...)`.
```cs
double GetTaxRate(IHazelcastInstance instance, int zipCode)
{
var taxRates = instance.GetMap("TaxRates");
if (!taxRates.TryGet(zipCode, out var taxRate))
{
throw new ArgumentException($"No tax rate defined for zip code '{zipCode}'.", nameof(zipCode));
}
return taxRate;
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.