Last active
October 30, 2024 05:08
-
-
Save azunyuuuuuuu/235a556b434acb4dc571cdb2b7ca612f to your computer and use it in GitHub Desktop.
personal c# snippet collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sealed class DisposableAction(Action action) : IDisposable | |
| { | |
| private readonly Action _action | |
| = action ?? throw new ArgumentNullException(nameof(action)); | |
| public void Dispose() | |
| => _action(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // source: https://stackoverflow.com/a/39997157 | |
| public static class ForeachWithIndex | |
| { | |
| // use .NET 9's .Index() | |
| public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source) | |
| => source.Select((item, index) => (item, index)); | |
| public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source) | |
| => source?.Select((item, index) => (item, index)) ?? Enumerable.Empty<(T, int)>(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Numerics; | |
| namespace dummycodes; | |
| // source: https://youtu.be/NzjF1pdlK7Y?t=1745 | |
| public static class Maths | |
| { | |
| // replace the Lerp with <type>.Lerp | |
| public static T InvLerp<T>(T a, T b, T value) | |
| where T : IFloatingPointIeee754<T> | |
| => (value - a) / (b - a); | |
| public static T Remap<T>(T inputmin, T inputmax, T outputmin, T outputmax, T value) | |
| where T : IFloatingPointIeee754<T> | |
| => T.Lerp(outputmin, outputmax, InvLerp(inputmin, inputmax, value)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment