Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't Support Collection Literals #2

Closed
SorenSaket opened this issue Sep 9, 2024 · 2 comments
Closed

Doesn't Support Collection Literals #2

SorenSaket opened this issue Sep 9, 2024 · 2 comments

Comments

@SorenSaket
Copy link

BiDirectionalDict doesn't support Collection Literals since theres no Add method.

To solve this either rename AddOrUpdate() to Add() or create a new Add() method.

To learn More:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions

@TwentyFourMinutes
Copy link
Owner

@SorenSaket You are correct, however BiDirectionalDict requires a Key and Value, like normal dictionaries. Collection Literals do, at the time of writing, not support Dictionaries. See dotnet/csharplang#7822.

@SorenSaket
Copy link
Author

Theres a difference between a collection literal and a dictionary literal. There's still advantages in implementing support for collection literals.

Implementation of Collection literals would allow for this:

BiDictionary<int, int> test = [
    new  (1,1)
    new  (21,3)
];

Support can be added by simply adding this to BiDictionary.cs:

public void Add((TFirst, TSecond) value)
{
  if (!_firstToSecond.ContainsKey(value.Item1))
  {
  _firstToSecond[value.Item1] = value.Item2;
  }
  
  if (!_secondToFirst.ContainsKey(value.Item2))
  {
  _secondToFirst[value.Item2] = value.Item1;
  }
}

The code works because the function is named add and takes a single tuple as input. You can alternativly implement as KeyValuePair instead of tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants