patterncsharpMinor
Primitive Extensions - Replaces static primitive methods
Viewed 0 times
extensionsprimitivemethodsreplacesstatic
Problem
I got annoyed having to do things like
I don't suppose there is much to critique about the code itself, as most methods are one-liners, but I'm wondering if what I have done violates any best-practice principles. Is there some insightful reasoning behind why these methods were left out in the first place?
The class dealing with strings is also very long, as I separated the classes based on which primitive they operated on, so all the
CharExtensions.cs
```
using System.Globalization;
namespace PrimitiveExtensions
{
public static class CharExtensions
{
public static double GetNumericValue(this char c)
{
return char.GetNumericValue(c);
}
public static UnicodeCategory GetUnicodeCategory(this char c)
{
return char.GetUnicodeCategory(c);
}
public static bool IsControl(this char c)
{
return char.IsControl(c);
}
public static bool IsDigit(this char c)
{
return char.IsDigit(c);
}
public static bool IsHighSurrogate(this char c)
{
return char.IsHighSurrogate(c);
}
public static bool IsLetter(this char c)
{
return char.IsLetter(c);
}
public static bool IsLetterOrDigit(this char c)
{
return char.IsLetterOrDigit(c);
}
public static bool IsLower(this char c)
{
return char.IsLower(c);
}
public static bool IsLowSurrogate(this char c)
{
string.IsNullOrEmpty(myString) when it seemed as if myString.IsNullOrEmpty() would perfectly suffice. Therefore, I wrote a small, simple library to wrap the static methods of primitives in extension methods.I don't suppose there is much to critique about the code itself, as most methods are one-liners, but I'm wondering if what I have done violates any best-practice principles. Is there some insightful reasoning behind why these methods were left out in the first place?
The class dealing with strings is also very long, as I separated the classes based on which primitive they operated on, so all the
Parse and TryParse methods belong to the string class. I also further wrapper the Parse methods, making private ParsePrimitive and public ToPrimitive methods. Is that an unnecessary layer of confusion?CharExtensions.cs
```
using System.Globalization;
namespace PrimitiveExtensions
{
public static class CharExtensions
{
public static double GetNumericValue(this char c)
{
return char.GetNumericValue(c);
}
public static UnicodeCategory GetUnicodeCategory(this char c)
{
return char.GetUnicodeCategory(c);
}
public static bool IsControl(this char c)
{
return char.IsControl(c);
}
public static bool IsDigit(this char c)
{
return char.IsDigit(c);
}
public static bool IsHighSurrogate(this char c)
{
return char.IsHighSurrogate(c);
}
public static bool IsLetter(this char c)
{
return char.IsLetter(c);
}
public static bool IsLetterOrDigit(this char c)
{
return char.IsLetterOrDigit(c);
}
public static bool IsLower(this char c)
{
return char.IsLower(c);
}
public static bool IsLowSurrogate(this char c)
{
Solution
I would probably go to jail for this :)
Where:
using static StringTest;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test" == NullOrEmpty);
Console.WriteLine("" == NullOrWhiteSpace);
Console.WriteLine(null == NullOrWhiteSpace);
}
}Where:
class StringTest
{
public static readonly StringTest NullOrEmpty = new StringTest(d => string.IsNullOrEmpty(d));
public static readonly StringTest NullOrWhiteSpace = new StringTest(d => string.IsNullOrWhiteSpace(d));
public StringTest(Predicate predicate)
{
Predicate = predicate;
}
Predicate Predicate { get; }
public static bool operator==(StringTest left, string right) =>
left.Predicate(right);
public static bool operator==(string left, StringTest right) =>
right.Predicate(left);
public static bool operator!=(StringTest left, string right) =>
!left.Predicate(right);
public static bool operator!=(string left, StringTest right) =>
!right.Predicate(left);
}Code Snippets
using static StringTest;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test" == NullOrEmpty);
Console.WriteLine("" == NullOrWhiteSpace);
Console.WriteLine(null == NullOrWhiteSpace);
}
}class StringTest
{
public static readonly StringTest NullOrEmpty = new StringTest(d => string.IsNullOrEmpty(d));
public static readonly StringTest NullOrWhiteSpace = new StringTest(d => string.IsNullOrWhiteSpace(d));
public StringTest(Predicate<string> predicate)
{
Predicate = predicate;
}
Predicate<string> Predicate { get; }
public static bool operator==(StringTest left, string right) =>
left.Predicate(right);
public static bool operator==(string left, StringTest right) =>
right.Predicate(left);
public static bool operator!=(StringTest left, string right) =>
!left.Predicate(right);
public static bool operator!=(string left, StringTest right) =>
!right.Predicate(left);
}Context
StackExchange Code Review Q#131888, answer score: 5
Revisions (0)
No revisions yet.