Skip to content

pylypeiev/CSharpExtensionMethods

Repository files navigation

C# Extension Methods

#My C# Extension methods for collections and POCO objects. Enjoy! đź’Ą

You can install all of them via NUGET 🔨

NuGet version

NuGet version

Table of Contents:

Collections extension methods (Namespace - Pylypeiev.Extensions):

POCO extension methods (Namespace - Pylypeiev.Extensions):

XML extension methods (Namespace - Pylypeiev.Extensions.Xml):

Collections extension methods (Namespace - Pylypeiev.Extensions):

Array extension methods:

  • Clear - Sets a range of elements in an array to the default value of each element type.
  • Join - Concatenates the elements of an object array, using the specified separator between each element
  • ToArrayString - A simple string representation of an array(regular, 2d, jagged)

Dictionary extension methods:

  • AddIfNotContainsKey - Add an element with provided key to dictionary if this key is not exist yet
  • AddOrUpdate - Add an element with provided key to dictionary, if this key is exist - update value
  • ToTuple - Convert this dictionary to list of Tuples, where item1 is key and item2 is a value from this dictionary
  • GetValueOrDefault - Get an element with provided key if this key is exist, otherwise default value
  • AsReadOnly - Wrap a dictionary in read only collection

ICollection extension methods:

  • AddIfNotContains - Add an element to the end of the collection if this value is not exist yet
  • AddRange - Adds the elements of the specified Array to the end of the collection
  • IsNullOrEmpty - Check if collection is null or empty
  • RemoveRange - Removes a range of elements from collection

IEnumerable extension methods:

  • Append - Add the object top the end of IEnumerable
  • AreAllSame - Check if all elements in IEnumerable are equals
  • Concatenate - Concatenates the elements of an IEnumerable to 1 string
  • ForEach - Performs the specified action on each element of the IEnumerable
  • IsEmpty - Check if IEnumerable is empty
  • IsNotEmpty - Check if IEnumerable is not empty
  • IsNullOrEmpty - Check if IEnumerable is null or empty
  • Join - Concatenates the elements of an IEnumerable, using the specified separator between each element
  • Prepend - Add the object at the beginning of IEnumerable
  • Shuffle - Shuffle IEnumerable
  • PickRandom - Pick N random elements from IEnumerable
  • ThisOrEmpty - Safe foreach and more, returns an empty Enumerable if source is null

IList extension methods:

  • ChunkBy - Chunk a list to smaller lists with a maximum capacity of the chunk size
  • Clone - Clone an collection to new IList
  • Push - Adds an object to the collection and return this collection for fluent api
  • GetPermutations - Get all permutations for this List
  • AsReadOnly - Wrap a list in read only collection

POCO extension methods (Namespace - Pylypeiev.Extensions):

bool extension methods:

  • ToYesNo - convert a boolean value to string representation

ByteArray extension methods:

  • ToBase64String - Converts an byte array to its equivalent string representation that is encoded with base-64.

DateTime extension methods:

  • Between - Check if date is between 2 dates. Inclusive
  • CalculateAge - > Calculate age
  • GetTime - Get time in numbers format
  • IsFuture - Check if this date is future
  • IsPast - Check if this date is past
  • IsDefault - Check if this DateTime is default, empty value
  • ToUnixTime - convert date to UNIX Epoch time format
  • TrimToSecond - Trims millisecond component so that the precision of the resulting time is to the second
  • TrimToMinute - Trims the second and millisecond components so that the precision of the resulting time is to the minutes
  • TrimToHour - Trims the minute, second and millisecond components so that the precision of the resulting time is to the hour
  • TrimToDay - Trims the time components so that the precision of the resulting time is to the day
  • GetQuarter - Determines the quarter to which the date belongs

decimal extension methods:

  • Abs - Returns the absolute value of this number
  • Ceiling - Returns the smallest integral value that is greater than or equal to the specified number
  • Floor - Returns the largest integral value that is greater than or equal to the specified number
  • GetPercentage - Calculate percentage from this number
  • IsBetween - Check if this number is between 2 numbers
  • Max - Returns the larger of two numbers
  • Min - Returns the smaller of two numbers
  • Round - Rounds a decimal value to the nearest integral value
  • Plus - Add a number to this number
  • Minus - Subtract a number from this number
  • DivideBy - Divide this number by given number
  • MultiplyBy - Multiply this number by given number

double extension methods:

  • Abs - Returns the absolute value of this number
  • Ceiling - Returns the smallest integral value that is greater than or equal to the specified number
  • Floor - Returns the largest integral value that is greater than or equal to the specified number
  • GetPercentage - Calculate percentage from this number
  • IsBetween - Check if this number is between 2 numbers
  • Max - Returns the larger of two numbers
  • Min - Returns the smaller of two numbers
  • Round - Rounds a decimal value to the nearest integral value
  • Plus - Add a number to this number
  • Minus - Subtract a number from this number
  • DivideBy - Divide this number by given number
  • MultiplyBy - Multiply this number by given number

float extension methods:

  • Abs - Returns the absolute value of this number
  • GetPercentage - Calculate percentage from this number
  • IsBetween - Check if this number is between 2 numbers
  • Max - Returns the larger of two numbers
  • Min - Returns the smaller of two numbers
  • Plus - Add a number to this number
  • Minus - Subtract a number from this number
  • DivideBy - Divide this number by given number
  • MultiplyBy - Multiply this number by given number

Func extension methods:

  • Or - Combines two Func or Predicates to form a logical OR
  • And - Combines two Func or Predicates to form a logical AND

int extension methods:

  • GetPercentage - Calculate percentage from this number
  • IsBetween - Check if this number is between 2 numbers
  • Max - Returns the larger of two numbers
  • Min - Returns the smaller of two numbers
  • ToFileSize - Format file size in KB/MB/GB/TB/PB/EB
  • Plus - Add a number to this number
  • Minus - Subtract a number from this number
  • DivideBy - Divide this number by given number
  • MultiplyBy - Multiply this number by given number

long extension methods:

  • GetPercentage - Calculate percentage from this number
  • IsBetween - Check if this number is between 2 numbers
  • Max - Returns the larger of two numbers
  • Min - Returns the smaller of two numbers
  • ToFileSize - Format file size in KB/MB/GB/TB/PB/EB
  • Plus - Add a number to this number
  • Minus - Subtract a number from this number
  • DivideBy - Divide this number by given number
  • MultiplyBy - Multiply this number by given number

object extension methods:

  • GetPropertiesWithValues - Get properties and values of this object using reflection
  • IfNotNull - Perform action/function on the object if it not null
  • IsDefaultValue - Check if object equals to its a default value
  • IsIn - Determines if an object is contained in a sequence
  • IsNotNull - Check if object is NOT null
  • IsNull - Check if object is null
  • ToStringSafe - like ToString of the object, but not crushes if the object is null
  • Try - Try to perform action/function on object
  • Yield - Wraps this object instance into an IEnumerable, consisting of a single item
  • DeepCopy - Deep copy of object using BinaryFormatter

string extension methods:

  • ToDateTime - Convert to DateTime
  • ToDecimal - Convert to decimal
  • ToDouble - Convert to double
  • ToFloat - Convert to float
  • ToInt - Convert to int
  • ToLong - Convert to long
  • ToByteArray - Converts a string into an byte array
  • DecodeBase64 - Decodes a string encoded in base-64
  • EncodeBase64 - Encodes a string to its equivalent string representation that is encoded in base-64
  • ToMd5 - Computes MD5 hash of a string.
  • RemoveFirst - Remove the number of characters at the start of this string
  • RemoveFirstCharacter - Remove the first character of this string
  • RemoveLast - Remove the number of characters at the end of this string
  • RemoveLastCharacter - Remove the last character of this string
  • Reverse - Reverse the sequence of characters in this string
  • SplitCamelCase - Split CamelCase words
  • SurroundWith - Surround this string with some string
  • ToTitleCase - Converts the specified string to title case
  • TrimSafe - Performs a trim only if the item is not null
  • ToLowerSafe - Performs ToLower() only if input is not null
  • ToLowerInvariantSafe - Performs ToLowerInvariant() only if input is not null
  • ToUpperSafe - Performs ToUpper() only if input is not null
  • ToUpperInvariantSafe - Performs ToUpperInvariant() only if input is not null
  • EndsWithIgnoreCase - Determines whether the end of this string matches the specified string
  • EqualsIgnoreCase - Determines whether two specified strings have the same value
  • IfNullThen - Determines if the string is null or whitespace if yes returns nullAlternateValue
  • IsAlpha - Determines if this string contains only English letters
  • IsAlphaNumeric - Determines if this string contains only English letters and digits
  • IsNullOrEmpty - Indicates whether this string is null or an empty string
  • IsNullOrWhiteSpace - Indicates whether this string is null, empty, or consists only of white-space characters
  • IsNumeric - Determines if this string contains only digits
  • IsPalindrome - Check if this string is a palindrome
  • IsValidEmail - Determines if this string is a valid email address
  • IsValidIP - Determines if this string is a valid IP address
  • NthIndexOf - Reports the index of matched string regards to occurrenceNum
  • OccurrenceNum - Reports the numbers of matches in this string
  • StartsWithIgnoreCase - Determines whether the beginning of this string matches the specified string
  • ContainsInvariantSafe - String.Contains with StringComparison parameter
  • SaveAs - write string to file

char extension methods:

Exception extension methods:

XML objects extension methods (Namespace - Pylypeiev.Extensions.Xml):

XmlDocument extension methods:

Xml.Linq extension methods:

Releases

No releases published

Packages

No packages published

Languages