hazelcast / hazelcast/hazelcast-csharp-client

Add a bool TryGet(TKey key, out TValue value) method to IMap [API-2049]

Open
#191 3 comments 1 reaction 0 assignees View on GitHub
Jira Priority: Low Source: Community Type: Enhancement
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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.