VOOZH about

URL: https://www.nuget.org/packages/DynamicSugarStandard/

⇱ NuGet Gallery | DynamicSugarStandard 1.0.90




DynamicSugarStandard 1.0.90

dotnet add package DynamicSugarStandard --version 1.0.90
 
 
NuGet\Install-Package DynamicSugarStandard -Version 1.0.90
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicSugarStandard" Version="1.0.90" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DynamicSugarStandard" Version="1.0.90" />
 
Directory.Packages.props
<PackageReference Include="DynamicSugarStandard" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DynamicSugarStandard --version 1.0.90
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DynamicSugarStandard, 1.0.90"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DynamicSugarStandard@1.0.90
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DynamicSugarStandard&version=1.0.90
 
Install as a Cake Addin
#tool nuget:?package=DynamicSugarStandard&version=1.0.90
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DynamicSugar.net /Standard

What is DynamicSugar.Net?

DynamicSugar.net a C# Library which provides methods and classes inspired by the dynamic languages

  • Python
  • JavaScript to write shorter and more readable source code in C#.

Created in 2011 for C# v 4.0 and .NET 4.0.

Videos

2023.

  • From 2011 to 2020, the C# language evolved and some features in DynamicSugar became obsolete, like the DynamicSugar string interpolation feature. But most of the features are still useful and I want to use them in my .NET 4.6.1 and .NET Core development in 2023 and 2024.

  • In 2023 I moved the library to .NET Standard 2.0, so it can be used with .NET 4.6.1 and .NET Core.

Examples:

List

// Quick and clean way to create lists
var intList = DS.List(1,2,3);
var intString = DS.List("a","b");
var l1 = DS.Range(10); // Range of number like in Python

// New list methods
var b = l1.Include(5);
var l = DS.List(1, 2, 3).Without( DS.List(3) );
var b = l1.IsEmpty();
var b = l1.IsNullOrEmpty();
var l = DS.List(1, 2, 3).Filter( e => e % 2 == 0 );
var l = DS.List(1, 2, 3).Merge( DS.List(3, 4, 5) );
var l = DS.List(1, 2, 3).Substract( DS.List(3, 4, 5) );
var l = DS.List(1, 2, 3).Intersect( DS.List(3, 4, 5) );
var l = DS.List(1, 2, 3).Identical( DS.List(1, 2, 3) );
var l = DS.List(1, 2, 3).Reject( e => e % 2 == 0 );
var l = DS.List(1,2,3,4).Map( e => e * e );
var l = DS.List(1,2,3,4).ToFile(@"c:\temp\list.txt", create: true);
var l = DS.List<string>().FromFile(fileName);
var l = DS.List<>(1,2,3).FromFile(fileName);

// Format
var l1 = DS.List(1,2,3);
Assert.AreEqual(@"1, 2, 3", DS.List(1, 2, 3).Format());
Assert.AreEqual(@"[1]:[2]:[3]", DS.List(1, 2, 3).Format("[{0}]", ":"));

// Cleaner syntax than Contains()
int i = 1;
if(i.In(1,2,3)) {
 // ...
}
var l = DS.List(1,2,3);
if(i.In(l)) {
 // ...
}

// Other Helpers
var l = DS.Array(1, 2, 3);
var l = DS.Queue(1, 2, 3);
var l = DS.Stack(1, 2, 3);

Dictionary


// Quick and clean way to create dictionary of <string, object>
var dic = DS.Dictionary(new { i = 1, f = 1.1f , s = "string", b = true });

// Get all the properties of one POCO into a Dictionary
var dic = DS.Dictionary(TestDataInstanceManager.TestPersonInstance);
Assert.AreEqual("TORRES", dic["LastName"]);
Assert.AreEqual(45, dic["Age"]);

// Include private property
var dic = DS.Dictionary(TestDataInstanceManager.TestPersonInstance, allProperties: true);

// Clone
var d2 = ReflectionHelper.CloneDictionary<string, object>(d);

// Dictionary includes sub Dictionary 
var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3, d = 4, e = 5 });
Assert.IsTrue(dic1.Include(dic1));

// Merge
var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3 });
var dic2 = DS.Dictionary(new { d = 4, e = 5 });
var dic3 = dic1.Merge(dic2); // dic1 is not change

// Format
var dic = DS.Dictionary(new { i = 1, f = 1.1f , s = "string", b = true});
Assert.AreEqual(@"{ i:1, f:1.1, s:""string"", b:True }", dic.Format());

// Custom format
var a = dic.Format("{0} ~ {1}", ",", "<", ">");
Assert.AreEqual(@"<i ~ 1,f ~ 1.1,s ~ ""string"",b ~ True>", dic.Format("{0} ~ {1}", "," , "<", ">"));

var d1 = DS.Dictionary( new { a=1, b=2, c=3 } );
Assert.IsTrue(DS.DictionaryHelper.Identical<string,object>(d1,d1));

// File
var d1 = DS.Dictionary( new { a=1, b=2, c=3 } );
d1.ToFile(@"c:\temp\dic.txt", create: true);

var d2 = new Dictionary<string, int>() { ["e"] = 100 };
d2.FromFile(@"c:\temp\dic.txt");

var d3 = (new Dictionary<string, int>()).FromFile(fileName);

// Get a dictionary of the properties types for a POCO
var testInstance = new TypeTestClass();
var dicType = DynamicSugar.ReflectionHelper.GetDictionaryWithType(testInstance);
Assert.AreEqual("String", dicType["LastName"]);
Assert.AreEqual("Int32", dicType["Age"]);

var dsi = new Dictionary<string, int>();
Type keyType, valueType;
ReflectionHelper.GetDictionaryType(dsi.GetType(), out keyType, out valueType);
Assert.AreEqual(typeof(string), keyType);
Assert.AreEqual(typeof(int), valueType);

Reflection and Property


// Just getting one property, including static property though you need to pass an instance
var lastName = ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "LastName").ToString();

var b = ReflectionHelper.MethodExist(TestDataInstanceManager.TestPersonInstance, "GetLastName");

var privateTitle = ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "PrivateTitle", isPrivate: true);

// Retrieve the type of a generic list or dictionary
var li = new List<int>();
Assert.AreEqual(typeof(int), ReflectionHelper.GetListType(li.GetType()));

// Get or set indexer property
var f = new IndexerTestClass();
ReflectionHelper.SetIndexer(f, 2, 123);
Assert.AreEqual(2 * 123, ReflectionHelper.GetIndexer(f, 1));

String Processing


// Deprecated since we have now string interpolation, but still works
var LastName = "TORRES";
var Age = 45;
var s1 = "LastName:{LastName}, Age:{Age:000}".Template( new { LastName, Age } );

// Template
s1 = "LastName:[LastName], Age:[Age]".Template(new { LastName, Age }, "[", "]");

String methods



Assert.AreEqual(123, "123".ToInt(defaultValue: -1));
Assert.AreEqual(new Guid("2E36429B-2695-4CB3-BCF2-9C7C6DC56B45"), "{2E36429B-2695-4CB3-BCF2-9C7C6DC56B45}".ToGuid());
Assert.AreEqual(new DateTime(1964, 12, 11), "12/11/1964".ToDateTime());
Assert.AreEqual(false, "false".ToBool(defaultValue: false));

Assert.AreEqual("Abcd", "abcd".Capitalize());
Assert.AreEqual("Abcd", "ABCD".Capitalize());

Assert.AreEqual("BCD", "ABCD".RemoveFirstChar());
Assert.AreEqual("ABC", "ABCD".RemoveLastChar());

Assert.AreEqual("terday I was here", "yesterday I was here".RemoveIfStartsWith("yes"));
Assert.AreEqual("yesterday I was ", "yesterday I was here".RemoveIfEndsWith("here"));

var text = "A\r\nB\r\nC";
var lines = text.SplitByCRLF();
Assert.AreEqual(3, lines.Count);

var text = "A \r\nB \r\nC ";
var lines = text.SplitByCRLF().TrimEnd();

var text = "A \r\nB \r\nC ";
var lines = text.SplitByCRLF().TrimEnd().Indent(" ", skipFirstOne: true);
Assert.AreEqual(3, lines.Count);
Assert.AreEqual("A", lines[0]);
Assert.AreEqual(" B", lines[1]);
Assert.AreEqual(" C", lines[2]);

string s = "";
Assert.AreEqual("default", s.IfNullOrEmpty("default"));
string s = null;
Assert.AreEqual("default", s.IfNull("default"));

string s = null;
Assert.IsTrue(s.IsNullOrEmpty());

string s = "";
Assert.IsTrue(s.IsEmpty());

var s = "ABCD";
Assert.AreEqual("BCD" , s.Slice(1));
Assert.AreEqual("BC" , s.Slice(1,2));

Assert.AreEqual("DCBA", "ABCD".Reverse());

// Removing comment C like comment
var result = $"[/* comment */]".RemoveComment(commentType: ExtensionMethods_Format.StringComment.C);

// Removing comment Python and Powershell like comment
var result = @"print(""Hello World"") # a comment".RemoveComment(commentType: ExtensionMethods_Format.StringComment.Python);

// Removing comment SQL like comment
var result = @"select * from customers -- a comment".RemoveComment(commentType: ExtensionMethods_Format.StringComment.SQL);

Unit Test Helper


// Comparing the properties of 2 POCO
var o = new { a = 1, b = 2, c = "ok", d = true, e = DateTime.Now, f = 1.2, g = 1.2M, h = 1.2f };
DS.Assert.AreEqualProperties(o, o);

// Comparing 2 Dictionary<string, object> 
var o = new { a = 1, b = 2, c = "ok", d = true, e = DateTime.Now, f = 1.2, g = 1.2M, h = 1.2f };
DS.Assert.AreEqualProperties(DS.Dictionary(o), DS.Dictionary(o));

// Assert a string contains some sets of words
DS.Assert.Words("aa bb", "(aa | bb) & (bb | aa)");

Resource files


// Retreive the content of a text file embedded as a resource
var alphabet = DS.Resources.GetTextResource("Alphabet.txt", Assembly.GetExecutingAssembly());

// Retreive multiple content of a text files embedded as a resource
Dictionary<string, string> alphabetDic = DS.Resources.GetTextResource(new Regex("DataClasses.Alphabet", RegexOptions.IgnoreCase), Assembly.GetExecutingAssembly());

// Retreive the content of a GZip text file embedded as a resource
var text = DS.Resources.GetTextResource("DS_Compression.txt.gzip", Assembly.GetExecutingAssembly(), true, DS.TextResourceEncoding.UTF8);

// Retreive the content of bitmap embedded as a resource
byte [] b = DS.Resources.GetBinaryResource("EmbedBitmap.bmp", Assembly.GetExecutingAssembly());

.NET GZip format, Zipping Unzipping file


var gzipFilename = DynamicSugar.Compression.GZip.GZipFile(fileName);
var newTextFileName = DynamicSugar.Compression.GZip.UnGZipFile(gzipFilename);

GZip.GZipFolder(string path, string wildCard);

License:

You may use DynamicSugar.Net under the terms of the MIT License.

NuGet

Install-Package DynamicSugarStandard

Blog Posts:

How to Write a Spelling Corrector? From Python to C# with Dynamic Sugar

Screen Casts:

Platforms:

  • Microsoft Windows and Windows Phone, .NET v 4.x
  • Xamarin iOS and Android

Outdated

  • The extension methods string.Format() and and string.Template() are now out dated, since C# support string interpolation.
  • The class MultiValues was an attempt to create functions returning multiple values, which is now supported by C#.

A lot of the features are still useful see Examples

License:

You may use DynamicSugar.Net under the terms of the MIT License.

NuGet

  • TODO: Create NuGet Package and publish
Install-Package DynamicSugarStandard

Platforms:

  • Microsoft Windows and Windows Phone, .NET v 4.x - 2011 - Source
  • Xamarin iOS and Android - 2011
  • Dot Net Standard 2.0 - 2019
Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 was computed. 
.NET Framework net461 net461 was computed.  net462 net462 was computed.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DynamicSugarStandard:

Package Downloads
fAI

Fred's experimentation with AI

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.90 137 6/14/2026
1.0.89 101 6/10/2026
1.0.88 3,077 5/13/2026
1.0.87 146 5/13/2026
1.0.86 2,467 3/29/2026
1.0.85 655 3/14/2026
1.0.84 261 12/4/2025
1.0.83 1,021 10/24/2025
1.0.82 230 10/8/2025
1.0.81 221 10/8/2025
1.0.80 213 10/7/2025
1.0.79 201 10/6/2025
1.0.78 205 10/5/2025
1.0.77 199 10/5/2025
1.0.76 196 10/5/2025
1.0.75 223 10/5/2025
1.0.74 202 10/5/2025
1.0.73 194 10/5/2025
1.0.72 188 6/29/2025
1.0.71 220 6/24/2025
Loading failed