DoNetExtensions 0.0.4
See the version list below for details.
dotnet add package DoNetExtensions --version 0.0.4
NuGet\Install-Package DoNetExtensions -Version 0.0.4
<PackageReference Include="DoNetExtensions" Version="0.0.4" />
<PackageVersion Include="DoNetExtensions" Version="0.0.4" />
<PackageReference Include="DoNetExtensions" />
paket add DoNetExtensions --version 0.0.4
#r "nuget: DoNetExtensions, 0.0.4"
#addin nuget:?package=DoNetExtensions&version=0.0.4
#tool nuget:?package=DoNetExtensions&version=0.0.4
The project aims to provide hundreds of fully-documented, tested, and useful extension methods to existing standard .NET classes, which we have accumulated through the years.
We have been coding with .NET for more than ten years, and we notice many standard classes (like array, list, dictionary, etc.) lack rich methods to facilitate their use and quick development. Although each piece extension method in this library is not much, we believe as a whole they bring great convenience and help enhance producitivity for other .NET programmers.
Initial Release
This initial release includes several extensions for arrays and collections as shown below. Currently all methods are under the same namespace as the classes they extend. Therefore, the usage is to just add reference to the extension library and then benefit from the added methods.
1. The "In" Method
Instead of "a.Contains(b)", we provide an alternative "b.In(a)", where "a" is a collection, and "b" is an element to check.
var a = new[] {1,2,3};
var b = 1;
if (b.In(a))
Do something.
In: returns true if the element to check is contained in an array/list/collection.
2. Collection to Array Conversion
var list = new List<int>();
var arr = list.ToArrayOrNull(); // returns a null reference
list.Add(1);
list.Add(2);
arr = list.ToArrayOrNull(); // returns an array [1,2]
arr = list.ToArrayThenClear(); // returns an array [1,2] and clears the list
arr = list.ToArrayOrNull(); // returns a null reference because list has been cleared.
ToArrayOrNull: returns a null reference if the collection is empty (rather than returns an empty array by the build-in ToArray() method), or otherwise works like build-in ToArray() method.
ToArrayThenClear: works like the build-in ToArray() method, but clears the collection after the elements are output to the array.
ToArrayOrNullThenClear: works like the ToArrayOrNull() method, but clears the collection after the elements are output to the array.
All added methods support conversion starting at a specified index.
3. Unified Emptiness Check
Although incredibly useful, the emptiness of an array or a collection has to be checked in a clumsy way, even for today after 10 years.
var arr = new int[] {1,2,3};
if (arr != null && arr.Length != 0) // NOTE: the new syntax "arr?.Length != 0" will not do the check as desired!
Do something...
var list = new List<int> {1,2,3};
if (list != null && arr.Count != 0) // NOTE: have to use a different property "Count"
Do something...
Now with the extension, above can be greatly simplified and more readable. The method is added to both arrays and collections.
var arr = new int[] {1,2,3};
if (arr.IsNotNullOrEmpty())
Do something...
var list = new List<int> { 1,2,3};
if (list.IsNotNullOrEmpty())
Do something...
var dict = new Dictionary<string, int> { { "a", 1 } }
if (dict.IsNotNullOrEmpty())
Do something...
IsNullOrEmpty: Returns true if a collection is a null reference or is an empty collection.
IsNotNullOrEmpty: Returns true if a collection is not a null reference or is not an empty collection.
IsEmpty: Returns true if a collection is an empty collection (throws an NullReferenceException if it is a null reference).
IsNotEmpty: Returns true if a collection is not an empty collection (throws an NullReferenceException if it is a null reference).
4. Collection to Concatenated String
ToConcatString: Outputs a concatenated string representation for elements in a collection. For each element, their ToString() method is used.
var arr = new int[] {1,2,3};
Console.WriteLine(arr.ToConcatString(',')); // prints "1,2,3"
Console.WriteLine(arr.ToConcatString("--")); // prints "1--2--3"
5. Basic Operations on Array
It is not uncommon that we might need to just add/remove one specified item to/from an array, and return a new array with the item added/removed (for example, such addition/removal is rarely used by the client, and it is not desirable to complicate the code design with other data structure like list or linked list).
var arr = new int[] {1,2,3};
var arr2 = arr.Remove(2); // returns a new array instance [1,3]
var arr3 = arr.RemoveAt(2); // returns a new array instance [1,2]
var arr4 = arr.AddFirst(0); // returns a new array instance [0,1,2,3]
var arr5 = arr.AddLast(4); // returns a new array instance [1,2,3,4]
var arr6 = arr.Insert(18, index:2); // returns a new array [1,2,18,3], with 18 inserted at position 2
var arr7 = arr.Insert(arr, index:2); // returns a new array [1,2,1,2,3,3], with "1,2,3" inserted at position 2
var merged = (new[] { arr, arr2, arr3, arr4, arr5, arr6, arr7}).Merge(); // merges all above arrays into on single array.
AddFirst: Returns a new array with one or more elements appended at the beginning of the current array.
AddLast: Returns a new array with one or more elements appended at the end of the current array.
Remove: Returns a new array with the specified element(s) removed.
RemoveAt: Returns a new array with the element at the specified index(es) removed.
Insert: Returns a new array with one or more elements inserted at the specified index.
Merge: Merges a collection of arrays into a single array.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.4.1)
- System.Dynamic.Runtime (>= 4.3.0)
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 (missing XML documentation added)