Skip to content

marioarce/PowerCSharp

PowerCSharp

PowerCSharp Banner

PowerCSharp License: MIT Build Status codecov NuGet Downloads Code Quality

Enhanced C# extension methods and utilities for .NET developers

NuGet NuGet NuGet NuGet NuGet NuGet

PowerCSharp is a comprehensive library of extension methods, utilities, and helper classes designed to enhance your C# development experience. Built by a senior C# architect with 20+ years of experience, this library provides practical, well-tested solutions for common programming challenges.

PowerCSharp v1.0.0 - Production Ready! πŸŽ‰

Major Release Highlights:

  • Production Stability: Comprehensive testing and validation for enterprise use
  • Complete API Surface: All extension methods, utilities, and helpers finalized
  • .NET 8.0 Optimization: Full support for latest .NET features and performance improvements
  • Semantic Versioning: Proper version management for long-term maintenance
  • Enhanced Documentation: Complete API reference and migration guides
  • NuGet Package Icons: Professional package presentation on NuGet Gallery

Recent Improvements (v1.0.0):

  • Production Release: Finalized all APIs for v1.0.0 stability
  • NuGet Package Icons: Added professional icons for all packages
  • Code Quality: Resolved nullable reference warnings and improved code coverage
  • Documentation Updates: Comprehensive v1.0.0 release notes and migration guides
  • Architecture Refactoring: Centralized interfaces in PowerCSharp.Core for better separation of concerns
  • Package Separation: Split ASP.NET Core extensions into dedicated package for cleaner dependencies
  • Enhanced Performance: Optimized implementations with reduced memory allocations
  • Improved Documentation: Comprehensive API documentation and usage examples
  • Better Testing: Expanded test coverage across all packages

πŸ“¦ Packages

PowerCSharp is organized into several focused packages:

πŸ—οΈ Architecture

PowerCSharp follows a clean architectural pattern with centralized interfaces in PowerCSharp.Core:

  • All interfaces are located in PowerCSharp.Core.Interfaces namespace
  • All models are located in PowerCSharp.Core.Models namespace
  • Clear separation of concerns with proper dependency management
  • Consistent namespace organization across the entire ecosystem
  • Modular design allowing selective package installation
  • Dependency-free core for maximum compatibility

πŸš€ Installation

Install individual packages via NuGet:

dotnet add package PowerCSharp.Core
dotnet add package PowerCSharp.Extensions
dotnet add package PowerCSharp.Extensions.AspNetCore
dotnet add package PowerCSharp.Utilities
dotnet add package PowerCSharp.Helpers
dotnet add package PowerCSharp.Compatibility

Or install the complete suite:

dotnet add package PowerCSharp.Core
dotnet add package PowerCSharp.Extensions
dotnet add package PowerCSharp.Extensions.AspNetCore
dotnet add package PowerCSharp.Utilities
dotnet add package PowerCSharp.Helpers
dotnet add package PowerCSharp.Compatibility

πŸ’‘ Usage Examples

String Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

string text = "hello world";
bool isEmpty = text.IsNullOrWhiteSpace(); // false
string title = text.ToTitleCase(); // "Hello World"
string safe = text.SafeSubstring(0, 5); // "hello"

// Additional string utilities
string camel = "HelloWorld".ToCamelCase(); // "helloWorld"
string firstLower = text.FirstCharToLowerCase(); // "hello world"
string mid = text.Mid(6); // "world"
string normalized = "User Name".NormalizeKey(); // "userName"
string ascii = "cafΓ©".AsAscii(); // "caf"
bool isValid = "https://example.com".IsValidUrl(); // true

Collection Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

var numbers = new List<int> { 1, 2, 3, 4, 5 };
bool isEmpty = numbers.IsNullOrEmpty(); // false
var first = numbers.FirstOrDefaultSafe(-1); // 1
var page = numbers.Page(1, 2); // [1, 2]

// New collection utilities
var list = new List<string> { "keep", "remove", "keep", "remove" };
int removed = list.RemoveAll(x => x == "remove"); // 2

DateTime Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

var date = DateTime.Now;
int age = date.GetAge();
bool isWeekend = date.IsWeekend();
var firstDay = date.FirstDayOfMonth();
var lastDay = date.LastDayOfMonth();

HTTP & Network Extensions (PowerCSharp.Extensions.AspNetCore)

using PowerCSharp.Extensions.AspNetCore;
using System.Net;

// HTTP Status Code utilities
HttpStatusCode status = HttpStatusCode.OK;
bool success = status.IsSuccessful(); // true
bool clientError = status.IsClientError(); // false
bool serverError = status.IsServerError(); // false
bool isRedirect = status.IsRedirect(); // false

// URI manipulation
Uri uri = new Uri("https://example.com");
Uri withParam = uri.AddParameter("search", "test"); // https://example.com?search=test

// HTTP Request cloning
using var request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com");
var clonedRequest = request.Clone();
var clonedAsync = await request.CloneAsync();

Configuration Extensions (PowerCSharp.Extensions.AspNetCore)

using PowerCSharp.Extensions.AspNetCore;
using Microsoft.Extensions.Configuration;

var configuration = new ConfigurationBuilder().Build();
var options = configuration.GetOptions<MyAppOptions>("MyApp"); // Reads from "MyApp" section

LINQ & Dynamic Query Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

// Dynamic expression parsing
string expression = "Age > 18 && Name.Contains('John')";
var predicate = expression.GetExpressionDelegate<Person>();

// Dynamic ordering
string orderExpression = "Name DESC, Age ASC";
var orderDelegates = orderExpression.GetOrderDelegates<Person>();

// Dynamic filtering and ordering
var filterProvider = new DynamicFilterProvider<Person>();
var orderProvider = new DynamicOrderProvider<Person>();
var filtered = people.Filter(filterProvider);
var ordered = people.Order(orderProvider);

JSON & XML Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;
using System.Text.Json;

// JSON element access
JsonElement element = JsonDocument.Parse("{\"name\":\"John\"}").RootElement;
var name = element.Get("name"); // JsonElement with value "John"
var firstItem = element.Get(0); // For arrays

// Case-insensitive JSON access
bool found = element.TryGetPropertyCaseInsensitive("NAME", out var value);

// XML flattening
using System.Xml.Linq;
XElement xml = XElement.Parse("<root><child>value</child></root>");
var dict = xml.Flatten(); // Dictionary with XML structure

Object & Type Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

// Object utilities
string text = "test";
text.ThrowOnNull(); // Throws if null

bool isTrue = "true".TryGetBool(out bool result); // result = true, isTrue = true
bool isFalse = "0".TryGetBool(out result); // result = false, isFalse = true

// Generic operations
var person = new Person { Name = "John", Age = 30 };
var copy = new Person();
person.CopyPropertiesTo(copy); // Copies matching properties

// Type operations
bool isDefault = default(int).IsDefault(); // true
string typeName = typeof(List<string>).GetGenericTypeName(); // "List<String>"

// Concrete type resolution
Type concreteType = typeof(IMyInterface).GetConcreteType();

Stream Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

using var originalStream = new MemoryStream(Encoding.UTF8.GetBytes("test data"));
using var destinationStream = new MemoryStream();

await originalStream.CloneAsync(destinationStream);
// destinationStream now contains the same data as originalStream

Object Hash Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

var person = new { Name = "John", Age = 30, Email = "john@example.com" };
string hash = person.ComputeHash(); // "A1B2C3D4E5F67890" (16-char hex string)

// Handles complex objects with nested properties
var complexObj = new Order 
{ 
    Id = 123, 
    Customer = new Customer { Name = "Alice" }, 
    Items = new List<Item> { new Item { Name = "Product1" } }
};
string orderHash = complexObj.ComputeHash(); // Consistent hash for caching/identification

Secure Path Extensions (PowerCSharp.Extensions)

using PowerCSharp.Extensions;

string basePath = "/var/www/uploads";
string userFile = "../../etc/passwd"; // Malicious attempt

// This will throw SecurityException due to directory traversal attempt
string safePath = PathExtensions.CombineAndValidate(basePath, userFile);

// Safe usage with valid relative paths
string validPath = PathExtensions.CombineAndValidate(basePath, "images/photo.jpg");
// Returns: "/var/www/uploads/images/photo.jpg"

// Multiple path segments
string multiPath = PathExtensions.CombineAndValidate(basePath, "documents", "2023", "report.pdf");
// Returns: "/var/www/uploads/documents/2023/report.pdf"

Validation Utilities (PowerCSharp.Utilities)

using PowerCSharp.Utilities;

bool isValidEmail = ValidationHelper.IsValidEmail("user@example.com");
bool isNumeric = ValidationHelper.IsNumeric("12345");
bool isValidUrl = ValidationHelper.IsValidUrl("https://example.com");

File Utilities (PowerCSharp.Utilities)

using PowerCSharp.Utilities;

string content = FileHelper.SafeReadAllText("file.txt");
bool success = FileHelper.SafeWriteAllText("output.txt", "Hello World");
string size = FileHelper.GetFileSize(1024 * 1024); // "1 MB"

JSON Helpers (PowerCSharp.Helpers)

using PowerCSharp.Helpers;

var obj = new { Name = "John", Age = 30 };
string json = JsonHelper.SafeSerialize(obj);
var deserialized = JsonHelper.SafeDeserialize<MyClass>(json);
string pretty = JsonHelper.PrettyPrint(json);

Crypto Helpers (PowerCSharp.Helpers)

using PowerCSharp.Helpers;

string sha256 = CryptoHelper.ComputeSHA256("password");
string md5 = CryptoHelper.ComputeMD5("data");
string random = CryptoHelper.GenerateRandomString(10);

🎯 Target Frameworks

  • Modern .NET: .NET 8.0 with full compatibility and latest features
  • Cross-platform: .NET Standard 2.0 (PowerCSharp.Core, Extensions, Helpers, Utilities)
  • .NET Framework: 4.6.2, 4.7.2, 4.8 (via PowerCSharp.Compatibility package)
  • ASP.NET Core: .NET 8.0 (PowerCSharp.Extensions.AspNetCore package)
  • Legacy Support: Seamless migration path from .NET Framework to modern .NET

πŸ”§ Recent Updates (v0.3.0)

  • Architectural Refactoring: Centralized interfaces in PowerCSharp.Core for better maintainability
  • Package Separation: Split ASP.NET Core extensions into dedicated package for cleaner dependencies
  • Performance Optimization: Reduced memory allocations and improved execution speed
  • Enhanced Testing: Expanded unit test coverage across all packages
  • Documentation Overhaul: Comprehensive API documentation and practical examples
  • Build System: Improved CI/CD pipeline with automated testing and packaging

πŸ§ͺ Testing

All PowerCSharp packages include comprehensive unit tests. Run tests with:

dotnet test

πŸ“š Documentation

Package-Specific Documentation

Detailed API Documentation

Development Documentation

🀝 Contributing

Contributions are welcome! Please read our Contributing Guidelines for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built with passion by Mario Arce
  • Inspired by 20+ years of C# development experience
  • Community feedback and contributions

πŸ“ž Support


PowerCSharp - Making C# development more powerful, one extension at a time! πŸš€

About

Enhanced C# extension methods and utilities for .NET developers

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages