Skip to content

urza/ExtensionMethods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 

Repository files navigation

ExtensionMethods - collection of useful C# collection methods

IEnumerableExtensions

  • ForEach : allows you to execute something for each of the items without any transformations/data manipulation
  • GetRandomElement : returns radnom element from the collection
  • MakeString(this IEnumerable chars) : Create string from IEnumerable of chars
  • TakeLast : Take last x elements from sequence (in order as they are in the sequence)
  • AddAlternatives : Create new collection that contains the original input and extends it with "alternative" items

BoolExtensions

  • In : Returns true if value is contained in given collection
  • Between : Returns true if the value is between lower and upper. Works for everything that implement IComparable e.g. Int, Double etc.
  • IsIncremental : Checks whether objects in this collection are in incremental order. (Objects must be comparable)

GenericExtensions

  • ToStringOrValue : Return this.ToString or alternative string value if this item does not satisfy the predicate condition

IntExtensions

  • Times : 5.Times(i => Console.writeLine(i));

EnumExtensions

  • GetDescription : Makes it easy to get the description (friendly name with spaces etc.) for a given value of the enum
  • GetDisplayName : Gets [Display(Name = "xxx")] of enum.

DateTimeExtensions

  • ToShortDateUnique : return .ToShortDate() or ShortDate+hour:minutes if there are two or more dates of the same day in list of dates
  • Quarter : Get the quarter of the year for this datetime.
  • FirstDayOfQUarter : First day of quarter to which this date belongs.
  • LastDayOfQUarter : Last day of quarter to which this date belongs.
  • StartOfWeek : Get first day of the week (that this date is part of).
  • ToDateOnly : Convert to DateOnly.

TimeExtensions

  • HoursToDecimal : 11:40 -> 11.6

NullableDateTimeExtensions

  • ToShortDateString : Returns short date string or empty string if the value is null
  • ToAge : Calculates age form date of birth

NullableExtensions

  • ToStringOrEmpty : Returns value of nullable type as string ot empty string if the value is null
  • double_ToStringOrEmpty : Reutrns double formated according to parameter or empty string

ListExtensions

  • Swap Swap two items in List

StringExtensions

  • IsDigitsOnly : Checks if the string consists of digits only
  • Println : Write string to console (System.Console.WriteLine(str);)
  • PrintlnAndPause Write string to console and wait for ReadKey
  • GetFirstLine First line from string
  • GetFirstWord
  • GetLines
  • ContainsAny "my text".ContainsAny("tems1", "term 2",...)
  • ContainsAll "my text".ContainsAll("tems1", "term 2",...)
  • RemoveDiacritics (experimental)
  • TryParseNumber : Parsing numbers as it should be in the framework.
  • TryParseNumberCzEn : trying to to go crazy from cz vs en decimal separator
  • MakeValidFileName Removes characters that are not valid in file names in Windows
  • SanitizeCzech
  • FirstCharToUpper Capitalize first letter
  • IsNotEmpty Instead of !string.IsNullOrEmpty(str) just do str.IsNotEmpty()
  • IsNotNullOrWhiteSpace Instead of !string.IsNullOrWhiteSpace(str) just do str.IsNotEmptyOrWhiteSpace()
  • Substring2 Substring without crashing when not long enough
  • ShortenCutMiddle Shorten the string to beginning..end
  • ShortenToLength Shorten the string to length and add "..."

DirectoryInfoExtensions

  • CopyTo : Copies all files from one directory to another

Helper methods

these are static methods, not extension methods

  • FirstNonEmpty : first string (from parameters) that is not null and not empty (e.g.: FirstNonEmpty(CustomColor, Person?.Color, "#6F3948")
  • CountDays : Counts how many Mondays (or Tuesdays,..) is in given date range
  • GetDaysBetween : Same but not enumareble of days, not count
  • monthsDatesBetween : months between two dates
  • monthsNamesBetween : months names between two dates
  • TryParseDate : parse datetime from string and returns null in case of fail.

UIExtensions (separete file Extensions_GUI.cs - needs WPF)

  • FindChild : UI child finder, e.g.: comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
  • GetScrollViewer : Returns ScrollViewer of this ListBox or null

About

My collection of useful C# extension methods

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages