Ecng.Common 1.0.239

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ecng.Common --version 1.0.239
                    
NuGet\Install-Package Ecng.Common -Version 1.0.239
                    
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="Ecng.Common" Version="1.0.239" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Common" Version="1.0.239" />
                    
Directory.Packages.props
<PackageReference Include="Ecng.Common" />
                    
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 Ecng.Common --version 1.0.239
                    
#r "nuget: Ecng.Common, 1.0.239"
                    
#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 Ecng.Common@1.0.239
                    
#: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=Ecng.Common&version=1.0.239
                    
Install as a Cake Addin
#tool nuget:?package=Ecng.Common&version=1.0.239
                    
Install as a Cake Tool

Ecng.Common

Core utilities and extension methods for everyday .NET development. Includes string helpers, type conversion, time utilities, and more.

String Utilities

Basic String Operations

using Ecng.Common;

// Check for null or empty
string text = GetText();
if (text.IsEmpty())
    return;

// Check for null, empty, or whitespace
if (text.IsEmptyOrWhiteSpace())
    return;

// Default value if empty
string value = text.IsEmpty("default value");

// Throw if empty
string required = input.ThrowIfEmpty(nameof(input));

// String formatting
string result = "{0} + {1} = {2}".Put(1, 2, 3);  // "1 + 2 = 3"

// Smart formatting with named parameters
string smart = "Hello {Name}!".PutEx(new { Name = "World" });

String Manipulation

// Join with separator
var items = new[] { "a", "b", "c" };
string joined = items.Join(", ");  // "a, b, c"

// Split by line separators (handles \r\n, \n, \r)
string[] lines = "line1\nline2\r\nline3".SplitByLineSeps();

// Case-insensitive comparison
bool equal = "ABC".EqualsIgnoreCase("abc");  // true
bool contains = "Hello World".ContainsIgnoreCase("world");  // true

// Remove characters
string cleaned = "hello123".Remove("123");  // "hello"

// Secure strings
SecureString secure = "password".Secure();
string plain = secure.UnSecure();

Validation

// Email validation
bool isEmail = "user@example.com".IsValidEmailAddress();

// URL validation
bool isUrl = "https://example.com".IsValidUrl();

Type Conversion

The Converter class provides flexible type conversion between many types.

Basic Conversion

// String to primitive types
int number = "42".To<int>();
double value = "3.14".To<double>();
bool flag = "true".To<bool>();
DateTime date = "2024-01-15".To<DateTime>();
Guid id = "550e8400-e29b-41d4-a716-446655440000".To<Guid>();

// With default value on failure
int safe = "invalid".To(defaultValue: 0);

// Between types
byte[] bytes = 12345.To<byte[]>();
long ticks = DateTime.Now.To<long>();

Network Types

// IP Address conversions
IPAddress ip = "192.168.1.1".To<IPAddress>();
string ipStr = ip.To<string>();
byte[] ipBytes = ip.To<byte[]>();
long ipLong = ip.To<long>();

// Endpoints
EndPoint endpoint = "192.168.1.1:8080".To<EndPoint>();
IPEndPoint ipEndpoint = "192.168.1.1:8080".To<IPEndPoint>();
DnsEndPoint dnsEndpoint = "example.com:443".To<DnsEndPoint>();

Custom Converters

// Register custom converter
Converter.AddTypedConverter<MyType, string>(obj => obj.ToString());
Converter.AddTypedConverter<string, MyType>(s => MyType.Parse(s));

// Use typed conversion
string str = myObject.TypedTo<MyType, string>();

CSV Parsing

FastCsvReader

High-performance, allocation-free CSV parser.

string csv = "Id;Name;Value\n1;Foo;100\n2;Bar;200";
var reader = new FastCsvReader(csv, ";");

while (reader.NextLine())
{
    int id = reader.ReadInt();
    string name = reader.ReadString();
    decimal value = reader.ReadDecimal();

    Console.WriteLine($"{id}: {name} = {value}");
}

Reading Different Types

var reader = new FastCsvReader(data, ",");

while (reader.NextLine())
{
    // Primitives
    int intVal = reader.ReadInt();
    long longVal = reader.ReadLong();
    double doubleVal = reader.ReadDouble();
    decimal decimalVal = reader.ReadDecimal();
    bool boolVal = reader.ReadBool();

    // Nullable types
    int? nullableInt = reader.ReadNullableInt();

    // Date/Time
    DateTime date = reader.ReadDateTime("yyyy-MM-dd");
    TimeSpan time = reader.ReadTimeSpan();

    // Enum
    MyEnum enumVal = reader.ReadEnum<MyEnum>();

    // Skip column
    reader.Skip();
}

Time Utilities

High-Precision Time

using Ecng.Common;

// High-precision current time (uses Stopwatch internally)
DateTime now = TimeHelper.Now;
DateTimeOffset nowWithOffset = TimeHelper.NowWithOffset;

// Adjust time offset (for testing or sync)
TimeHelper.NowOffset = TimeSpan.FromSeconds(5);

// Sync with NTP server
TimeHelper.SyncMarketTime(timeout: 5000);

TimeSpan Extensions

TimeSpan span = TimeSpan.FromDays(365);

double weeks = span.TotalWeeks();
double months = span.TotalMonths();
double years = span.TotalYears();

// Constants
long ticksPerWeek = TimeHelper.TicksPerWeek;
long ticksPerMonth = TimeHelper.TicksPerMonth;
long ticksPerYear = TimeHelper.TicksPerYear;

// Predefined spans
TimeSpan oneMinute = TimeHelper.Minute1;
TimeSpan fiveMinutes = TimeHelper.Minute5;
TimeSpan oneHour = TimeHelper.Hour1;

DateTime Extensions

DateTime dt = DateTime.Now;

// Truncation
DateTime dateOnly = dt.Truncate(TimeSpan.FromDays(1));
DateTime hourOnly = dt.Truncate(TimeSpan.FromHours(1));

// Apply timezone
DateTimeOffset local = dt.ApplyLocal();
DateTimeOffset utc = dt.ApplyUtc();
DateTimeOffset custom = dt.ApplyTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));

// Async delay
await TimeSpan.FromSeconds(1).Delay(cancellationToken);

I/O Utilities

File Operations

using Ecng.Common;

// Safe file operations
string content = IOHelper.ReadFile("path/to/file.txt");
IOHelper.WriteFile("path/to/file.txt", content);

// Atomic file write (writes to temp, then renames)
IOHelper.AtomicWriteFile("path/to/file.txt", content);

// Get relative path
string relative = IOHelper.GetRelativePath(basePath, fullPath);

Stream Extensions

// Read all bytes
byte[] data = stream.ReadToEnd();

// Copy with progress
await source.CopyToAsync(destination, progress: bytesWritten =>
{
    Console.WriteLine($"Written: {bytesWritten}");
});

Math Utilities

using Ecng.Common;

// Rounding
double rounded = 3.7.Round();      // 4
double ceiling = 3.1.Ceiling();    // 4
double floor = 3.9.Floor();        // 3

// Clamping
int clamped = 150.Max(100);        // 100
int clamped2 = 50.Min(100);        // 100

// Abs
int absolute = (-5).Abs();         // 5

// Percentage
decimal pct = 250m.Percent(1000m); // 25

Random Generation

using Ecng.Common;

// Random values
int randomInt = RandomGen.GetInt(1, 100);
double randomDouble = RandomGen.GetDouble();
bool randomBool = RandomGen.GetBool();

// Random bytes
byte[] randomBytes = RandomGen.GetBytes(32);

// Random string
string randomStr = RandomGen.GetString(16);

Disposable Helpers

Base Disposable Class

public class MyResource : Disposable
{
    private IntPtr _handle;

    protected override void DisposeManaged()
    {
        // Clean up managed resources
        base.DisposeManaged();
    }

    protected override void DisposeNative()
    {
        // Clean up native resources
        CloseHandle(_handle);
        base.DisposeNative();
    }
}

Disposable Scope

// Dispose multiple objects at once
using var scope = new DisposeScope(resource1, resource2, resource3);

// Or with extension
resource1.DisposeWith(scope);

File System Abstraction

IFileSystem Interface

// Local file system
IFileSystem fs = new LocalFileSystem();

// In-memory file system (for testing)
IFileSystem memFs = new MemoryFileSystem();

// Operations
bool exists = fs.FileExists("path/to/file");
byte[] data = fs.ReadAllBytes("path/to/file");
fs.WriteAllBytes("path/to/file", data);
fs.CreateDirectory("path/to/dir");
IEnumerable<string> files = fs.GetFiles("path", "*.txt");

Currency Support

// Currency types
CurrencyTypes usd = CurrencyTypes.USD;
CurrencyTypes eur = CurrencyTypes.EUR;

// Currency operations
Currency amount = new Currency(100, CurrencyTypes.USD);
string display = amount.ToString(); // "$100.00"

Cloning

// Deep clone
var clone = original.Clone();

// Typed clone
public class MyClass : Cloneable<MyClass>
{
    public override MyClass Clone()
    {
        return new MyClass { /* copy properties */ };
    }
}

Watch (Benchmarking)

using var watch = new Watch("Operation name");

// Do work...

// Automatically logs elapsed time on dispose
// Or get elapsed manually
TimeSpan elapsed = watch.Elapsed;

NuGet

Install-Package Ecng.Common
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Ecng.Common:

Package Downloads
Ecng.Collections

Ecng system framework

Ecng.Localization

Ecng system framework

Ecng.Configuration

Ecng system framework

Ecng.IO

Ecng system framework

Ecng.Backup

Ecng system framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.248 995 1/22/2026
1.0.247 2,234 1/19/2026
1.0.246 2,094 1/18/2026
1.0.245 1,268 1/18/2026
1.0.244 2,039 1/14/2026
1.0.243 1,357 1/13/2026
1.0.242 1,286 1/13/2026
1.0.241 1,803 1/9/2026
1.0.240 4,987 1/4/2026
1.0.239 3,067 12/30/2025
1.0.238 1,173 12/29/2025
1.0.237 1,632 12/26/2025
1.0.236 1,194 12/26/2025
1.0.235 1,177 12/26/2025
1.0.234 1,177 12/26/2025
1.0.233 1,305 12/25/2025
1.0.232 1,274 12/25/2025
1.0.231 1,786 12/22/2025
1.0.230 1,345 12/21/2025
1.0.229 1,427 12/19/2025
1.0.228 1,322 12/19/2025
1.0.227 1,487 12/17/2025
1.0.226 2,474 12/15/2025
1.0.225 3,157 12/12/2025
1.0.224 4,054 12/12/2025
1.0.223 4,375 11/29/2025
1.0.222 1,465 11/28/2025
1.0.221 1,461 11/28/2025
1.0.220 1,548 11/27/2025
1.0.219 1,648 11/24/2025
1.0.218 1,564 11/24/2025
1.0.217 1,554 11/23/2025
1.0.216 2,151 11/22/2025
1.0.215 2,712 11/20/2025
1.0.214 1,779 11/18/2025
1.0.213 1,710 11/18/2025
1.0.212 1,821 11/13/2025
1.0.211 1,677 11/10/2025
1.0.210 2,594 11/1/2025
1.0.209 1,854 10/28/2025
1.0.208 1,820 10/27/2025
1.0.207 1,705 10/27/2025
1.0.206 1,612 10/25/2025
1.0.205 5,341 10/3/2025
1.0.204 3,995 9/25/2025
1.0.203 10,286 8/30/2025
1.0.202 10,545 7/13/2025
1.0.201 1,596 7/13/2025
1.0.200 1,583 7/12/2025
1.0.199 3,081 7/8/2025
1.0.198 9,023 6/16/2025
1.0.197 1,823 6/9/2025
1.0.196 1,699 6/8/2025
1.0.195 3,327 5/21/2025
1.0.194 1,823 5/17/2025
1.0.193 3,434 5/12/2025
1.0.192 1,722 5/12/2025
1.0.191 4,398 4/17/2025
1.0.190 8,424 3/20/2025
1.0.189 1,613 3/19/2025
1.0.188 6,664 2/26/2025
1.0.187 1,734 2/26/2025
1.0.186 10,494 2/5/2025
1.0.185 5,703 1/21/2025
1.0.184 4,969 1/14/2025
1.0.183 3,788 1/12/2025
1.0.182 2,274 1/10/2025
1.0.181 11,198 11/18/2024
1.0.180 3,585 11/7/2024
1.0.179 2,963 10/19/2024
1.0.178 8,575 10/5/2024
1.0.177 6,605 9/18/2024
1.0.176 1,768 9/17/2024
1.0.175 7,007 9/1/2024
1.0.174 16,370 6/12/2024
1.0.173 4,626 5/28/2024
1.0.172 5,446 5/4/2024
1.0.171 6,976 4/14/2024
1.0.170 7,511 3/28/2024
1.0.169 2,094 3/17/2024
1.0.168 5,458 2/23/2024
1.0.167 2,052 2/23/2024
1.0.166 6,291 2/18/2024
1.0.165 2,170 2/16/2024
1.0.164 4,309 2/13/2024
1.0.163 4,021 2/8/2024
1.0.162 5,334 2/4/2024
1.0.161 4,656 1/23/2024
1.0.160 5,057 1/12/2024
1.0.159 7,361 1/2/2024
1.0.158 2,414 12/29/2023
1.0.157 20,835 11/12/2023
1.0.156 2,812 11/10/2023
1.0.155 2,282 11/10/2023
1.0.154 2,598 11/9/2023
1.0.153 3,390 11/3/2023
1.0.152 2,385 11/1/2023
1.0.151 2,393 11/1/2023
1.0.150 29,597 9/8/2023
1.0.149 2,929 9/8/2023
1.0.148 3,173 9/3/2023
1.0.147 3,406 8/21/2023
1.0.146 3,722 8/14/2023
1.0.145 4,022 8/10/2023
1.0.144 43,810 6/29/2023
1.0.143 18,330 5/27/2023
1.0.142 5,414 5/19/2023
1.0.141 29,179 5/8/2023
1.0.140 9,228 4/21/2023
1.0.139 55,770 4/3/2023
1.0.138 12,098 3/13/2023
1.0.137 23,509 3/6/2023
1.0.136 5,820 2/26/2023
1.0.135 63,811 2/9/2023
1.0.134 21,940 2/7/2023
1.0.133 6,085 2/4/2023
1.0.132 25,967 2/2/2023
1.0.131 22,586 1/30/2023
1.0.130 11,224 1/18/2023
1.0.129 52,588 12/30/2022
1.0.128 8,187 12/23/2022
1.0.127 26,855 12/12/2022
1.0.126 29,327 12/4/2022
1.0.125 6,361 12/4/2022
1.0.124 7,610 11/30/2022
1.0.123 10,545 11/28/2022
1.0.122 10,928 11/18/2022
1.0.121 34,685 11/11/2022
1.0.120 7,203 11/11/2022
1.0.119 6,771 11/10/2022
1.0.118 7,856 11/5/2022
1.0.117 8,624 11/4/2022
1.0.116 31,108 11/1/2022
1.0.115 36,782 10/16/2022
1.0.114 15,393 9/10/2022
1.0.113 59,085 9/8/2022
1.0.112 8,040 9/8/2022
1.0.111 8,102 9/8/2022
1.0.110 10,467 9/4/2022
1.0.109 98,550 8/24/2022
1.0.108 17,910 8/8/2022
1.0.107 11,164 7/26/2022
1.0.106 8,075 7/26/2022
1.0.105 60,746 7/19/2022
1.0.104 53,506 7/18/2022
1.0.103 14,127 7/8/2022
1.0.102 12,647 6/18/2022
1.0.101 8,211 6/6/2022
1.0.100 104,394 4/30/2022
1.0.99 8,089 4/20/2022
1.0.98 8,186 4/10/2022
1.0.97 8,100 4/7/2022
1.0.96 8,197 4/7/2022
1.0.95 8,229 4/2/2022
1.0.94 19,628 3/29/2022
1.0.93 11,049 3/27/2022
1.0.92 298,037 1/24/2022
1.0.91 168,841 12/29/2021
1.0.90 34,359 12/20/2021
1.0.89 7,076 12/13/2021
1.0.88 64,587 12/6/2021
1.0.87 8,672 12/2/2021
1.0.86 34,739 11/29/2021
1.0.85 33,515 11/22/2021
1.0.84 5,205 11/17/2021
1.0.83 35,858 11/13/2021
1.0.82 8,634 11/10/2021
1.0.81 5,447 11/9/2021
1.0.80 68,526 11/5/2021
1.0.79 7,757 11/4/2021
1.0.78 5,315 11/4/2021
1.0.77 5,239 11/3/2021
1.0.76 5,615 10/30/2021
1.0.75 37,137 10/21/2021
1.0.74 6,101 10/17/2021
1.0.73 67,634 10/14/2021
1.0.72 17,212 10/13/2021
1.0.71 6,011 10/12/2021
1.0.70 37,822 10/11/2021
1.0.69 5,510 10/9/2021
1.0.68 40,773 10/7/2021
1.0.67 42,915 10/7/2021
1.0.66 5,509 10/7/2021
1.0.65 5,504 10/6/2021
1.0.64 5,375 9/28/2021
1.0.63 39,467 9/23/2021
1.0.62 7,086 9/10/2021
1.0.61 5,165 9/9/2021
1.0.60 5,190 9/8/2021
1.0.59 5,233 9/8/2021
1.0.58 36,553 9/6/2021
1.0.57 5,512 8/31/2021
1.0.56 5,325 8/30/2021
1.0.55 38,679 7/31/2021
1.0.54 65,790 7/30/2021
1.0.53 5,522 7/26/2021
1.0.52 95,567 7/5/2021
1.0.51 5,458 7/1/2021
1.0.50 68,291 6/4/2021
1.0.49 96,805 4/26/2021
1.0.48 36,758 4/19/2021
1.0.47 155,860 4/7/2021
1.0.46 36,096 4/3/2021
1.0.45 185,498 3/22/2021
1.0.44 118,421 3/4/2021
1.0.43 38,588 2/26/2021
1.0.42 174,573 2/2/2021
1.0.41 125,072 1/24/2021
1.0.40 5,460 1/23/2021
1.0.39 63,911 1/20/2021
1.0.38 5,361 1/20/2021
1.0.37 37,724 1/18/2021
1.0.36 33,316 1/16/2021
1.0.35 124,398 12/16/2020
1.0.34 61,325 12/14/2020
1.0.33 38,545 12/9/2020
1.0.32 8,105 12/6/2020
1.0.31 9,951 12/2/2020
1.0.30 34,582 12/1/2020
1.0.29 198,464 11/12/2020
1.0.29-atestpub 2,635 11/11/2020
1.0.28 35,507 10/11/2020
1.0.27 118,267 9/9/2020
1.0.26 33,921 9/3/2020
1.0.25 34,415 8/20/2020
1.0.24 90,330 8/9/2020
1.0.23 34,824 7/28/2020
1.0.22 34,218 7/19/2020
1.0.21 61,197 7/6/2020
1.0.20 90,818 6/6/2020
1.0.19 35,503 6/4/2020
1.0.18 62,589 5/29/2020
1.0.17 62,699 5/21/2020
1.0.16 6,521 5/17/2020
1.0.15 61,246 5/12/2020
1.0.14 116,468 5/4/2020
1.0.13 10,537 4/24/2020
1.0.12 14,035 4/22/2020
1.0.11 6,812 4/22/2020
1.0.10 6,336 4/21/2020
1.0.9 36,120 4/18/2020
1.0.8 33,592 4/16/2020
1.0.7 6,302 4/16/2020
1.0.6 28,617 4/15/2020
1.0.5 31,591 4/11/2020
1.0.4 30,133 4/3/2020
1.0.3 5,633 4/1/2020
1.0.2 17,423 3/27/2020
1.0.1 16,474 3/22/2020
1.0.0 9,647 3/22/2020

IO refactoring.