IcIWare.NamedIndexers
1.0.0
See the version list below for details.
dotnet add package IcIWare.NamedIndexers --version 1.0.0
NuGet\Install-Package IcIWare.NamedIndexers -Version 1.0.0
<PackageReference Include="IcIWare.NamedIndexers" Version="1.0.0" />
paket add IcIWare.NamedIndexers --version 1.0.0
#r "nuget: IcIWare.NamedIndexers, 1.0.0"
// Install IcIWare.NamedIndexers as a Cake Addin #addin nuget:?package=IcIWare.NamedIndexers&version=1.0.0 // Install IcIWare.NamedIndexers as a Cake Tool #tool nuget:?package=IcIWare.NamedIndexers&version=1.0.0
NamedIndexers
A small utility library that introduces a generic proxy classes which simulate named indexers (also known as indexed properties) with user-supplied getter and optional setter functions through lambdas or delegates.
Rationale
Sometimes you may find it logical to have a class with two or more indexers, but it's not obvious from context what each does. Maybe your keys automatically cast into each other which creates ambiguity or you need two numeric indexers each using a different internal logic. You want to be clear what each indexer does but using getter and setter functions would be cumbersome and not so user friendly. Instead, this library allows you to create small proxy objects with their own indexers which refer back to their host object through lambda expressions.
Install
You can get it from NuGet under the name IcIWare.NamedIndexers
.
Alternatively you can download the DLL from the releases and simply add it as a reference.
Usage
You have two classes at your disposal inside the IcIWare.NamedIndexers
namespace:
- NamedIndexer : can be used for both getting and setting
- NamedGetter : getter only version
Create a
public readonly
field in your class. Initialize it in the constructor of the host class using lambda expressions. This way the object doesn't have to be aware of its host because the lambda's automatic closure takes care of it all.
public class ExampleObject
{
private int[] _numbers = new[] { 4, 8, 15, 16, 23, 42 };
public readonly NamedIndexer<int, int> TheNumbersButBackwards;
public ExampleObject()
{
TheNumbersButBackwards = new NamedIndexer<int, int>(
i => _numbers[_numbers.Length - 1 - i],
(i, x) => { _numbers[_numbers.Length - 1 - i] = x; });
}
public static Main(string[] args)
{
var example = new ExampleObject();
Console.WriteLine("The answer is {0}.", example.TheNumbersButBackwards[0]);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release.