https://github.com/joshua-light/pocket.net
A tiny pocket of useful stuff for .NET.
https://github.com/joshua-light/pocket.net
common declarative extensions fluent linq
Last synced: 9 months ago
JSON representation
A tiny pocket of useful stuff for .NET.
- Host: GitHub
- URL: https://github.com/joshua-light/pocket.net
- Owner: joshua-light
- License: mit
- Created: 2018-05-28T20:36:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-16T09:37:14.000Z (over 5 years ago)
- Last Synced: 2025-10-06T00:58:24.797Z (9 months ago)
- Topics: common, declarative, extensions, fluent, linq
- Language: C#
- Homepage:
- Size: 572 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pocket

[](https://www.nuget.org/packages/Pocket)
_A tiny pocket of useful stuff for .NET._
## Extensions
### Collections
#### `AddRange`
Adds a range of items to the `ICollection` instance (pretty common extension that is missing in standard library).
```cs
ICollection numbersA = { 1, 2, 3 };
IEnumerable numbersB = { 4, 5, 6 };
numbersA.AddRange(numbersB);
Console.WriteLine(numbersA.AsString()); // Prints "[1, 2, 3, 4, 5, 6]".
```
### Dictionary
#### `One`
Represents a consistent access to dictionary elements in four different ways using fluent sentences, starting with `One`. Each way represents a reaction to situation, when key is missing.
These reactions are:
a) throw a `KeyNotFoundException` with specified message:
```cs
var writers = new Dictionary
{
{ "Hermann": "Hesse" },
};
var x = writers.One(withKey: "Thomas").OrThrow(withMessage: "Couldn't find `Thomas`.");
// `KeyNotFoundException` is thrown.
```
b) return `default(TValue)`:
```cs
var writers = new Dictionary
{
{ "Hermann": "Hesse" },
};
var x = writers.One(withKey: "Thomas").OrDefault();
Console.WriteLine(x ?? "null"); // Prints `null`.
```
c) return specified value:
```cs
var writers = new Dictionary
{
{ "Hermann": "Hesse" },
};
var x = writers.One(withKey: "Thomas").Or("");
var y = writers.One(withKey: "Thomas").Or(() => "");
Console.WriteLine(x); // Prints `""`.
Console.WriteLine(y); // Prints `""`.
```
d) return specified value and also write it to dictionary:
```cs
var writers = new Dictionary
{
{ "Hermann": "Hesse" },
};
var x = writers.One(withKey: "Thomas").OrNew("Mann");
var y = writers["Thomas"];
Console.WriteLine(x); // Prints `"Mann"`.
Console.WriteLine(y); // Prints `"Mann"`.
```
### Enumerable
#### `OrEmpty`
Returns `Enumerable.Empty` sequence if current is `null`. This method can be used
as a more fluent alternative to `x ?? Enumerable.Empty`.
```cs
IEnumerable x = null;
Console.WriteLine(x.OrEmpty()); // Prints [].
```
#### `Each`
Executes specified action on each item of the sequence. This can be useful when you want,
for example, to print items in the middle of LINQ.
``` cs
Enumerable
.Range(1, 10)
.Where(x => x % 2 == 0)
.Each(Console.Write)
.Select(x => x * x)
.ToList(); // Prints "246810".
```
#### `ForEach`
Executes specified action on each item of the sequence, but doesn't return anything.
This method is similar to `ForEach` from `List`.
``` cs
Enumerable
.Range(0, 10)
.Where(x => x % 2 == 0)
.ForEach(Console.Write); // Prints "246810".
```
#### `MinBy`
Takes first object from the sequence that has minimum value, provided by specified selector function.
This method works in same way as LINQ `Min` but instead of value returns the object, not the minimum value it holds.
``` cs
struct Point
{
public readonly int X;
public readonly int Y;
public Point(int x, int y) =>
(X, Y) = (x, y)
}
// `point` holds not an `int`, but `Point`.
var point = Enumerable
.Range(0, 10)
.Select(x => new Point(x, x))
.MinBy(p => p.X)
Console.WriteLine($"{point.X}, {point.Y}") // Prints "0, 0".
```
#### `MaxBy`
Works in similar way as `MinBy`, but returns an object with maximum value.
#### `IsNullOrEmpty`
Checks whether sequence is either `null` or contains no elements.
``` cs
IEnumerable a;
a = Enumerable.Range(0, 10);
Console.WriteLine(numbers.IsNullOrEmpty()); // Prints "false".
a = null;
Console.WriteLine(numbers.IsNullOrEmpty()); // Prints "true".
a = Enumerable.Empty();
Console.WriteLine(numbers.IsNullOrEmpty()); // Prints "true".
```
#### `IsEmpty`
Checks whether sequence contains no elements.
``` cs
var sequence = Enumerable.Empty();
Console.WriteLine(sequence.IsEmpty()); // Prints "true".
```
TODO