A bunch of small utils and extensions which I often used in different projects on .NET.
- DateInterval: Class to store interval of two DateTime values. Allows to check intersection/including, equality and split interval by DateIntervalType.
- NumsRange: Class to store double range min-max. Contains NumsRangeUtil with few methods to create a list of NumsRange based on rules: by range size, by ranges amount.
- StringExtension:
- ToString: Common 'ToString' method with possibility to change empty value to default text;
- FirstCharToUpper: Change first char of input text to Upper;
- AddHtmlTag: Wrap string with a HTML tag;
- RemoveHtmlTags: Remove all HTML tags from string;
- CreateMultiLineByLength: Convert input text to multiline length with lines of specific row length. Preserve words;
- CreateMultiLineByDelimiters: Convert input text to multiline separated by delimiters;
- Append: Allow to append new line to a StringBuilder with delimeter (like spaces);
- ConvertFromBase64: Convert string from Base64 format to an UTF8.
- ColorExtension:
- GetRandomColor: Get nice random color. Use LRU cache to create unique colors;
- Lightend, Darken: Tints the color by the given percent;
- Invert: Color inversion;
- GenerateColorFromString: Generate color based on a string hash;
- DoubleExtension:
- Validate: Validate number if it has value (not nan or infinity). If empty - can return string/number default value;
- ToString: Get string representation with number validation. If empty - can return string/number default value;
- EqualsEpsilon, EqualsEpsilonNaN, EqualsEpsilonNanInf: equal two numbers with epsilon to fix floating-point comparison issue. Supports Nan and Infinitives to compare;
- RoundToSignificant: Round number up to significant digits;
- DateTimeExtension:
- IsMidnight: Check if date is midnight
- IsMonthStart: Check if date is month start
- IsQuarterStart: Check if date is quarter start
- IsYearStart: Check if date is year start
- GetQuarter: Get date quarter (number/arabic number)
- IsStartOfIntervalType: Check if date is start of DateInterval type (day/month/quarter/etc)
- AddByType/SubtractByType: Addition/Subtracting amount to a date by DateInterval type
- Ceil/Floor: Ceil/floor date by DateInterval
- GetDateInterval: Convert DateTime to DateInterval according to DateInterval type
- EnumExtension: Useful methods to work with enums:
- Allows to get caption/enum-value by DescriptionAttribute;
- Fill different DevExpress editors with enum values.
public enum TestEnum { [Description("Show angles on the map")] Angles, [Description("Show axis on the graph")] Axis } var caption = TestEnum.Angles.GetCaption();
- ConvertUtil. Some useful methods to extend a System.Convert library. All convert metods Process Unicode BOM symbols, DBull values.
- ToString: Convert an object to string. If object empty - can return string default value;
- ToBool: Convert an object/string to boolean. Supports different representation of 'true' value like 1, YES, Y;
- ToDateTime: Convert an object/string to DateTime. Supports both: default string representation of DateTime and OLE automation date (Excel);
- ToDouble: Convert object/string value to double with CurrentFormatInfo. Supports different delimiters, default value;
- ToInt: Convert object/string value to int. Supports different delimiters, default value.
- FileSystemUtil
- GetFileEncoding: Determines a text file's encoding by analyzing its byte order mark (BOM);
- LaunchFile: Launch file in an assigned application or show it in the Explorer;
- OpenFolder: Open a folder in the Explorer;
- ShowFileInExplorer: Open the Explorer and focus on a file;
- ValidateFileName: Validate and return valid filename;
- GetFileExtension: Get file extension for a file, encoded in Base64 string.
- DateTimeUtil
- Min/Max: Get bigger/smaller date between two of them;
- MonthsBetween: Get difference between two dates in months;
- Concat: Concat two lists of DateTime;
- ConvertDateIntervalsToDateTime: Convert list of DateInterval to list of DateTime;
- ConvertDateTimeToDateIntervals: Convert list of DateTime to list of DateInterval;
- Merge: Merge consecutive dates into one DateInterval.
- FormsUtil
- IsDesignMode: Check if a form opened in Designer mode;
- DPIUtil
- GetDpi: Get DPI: for a monitor, if OS supports DPI per monitor or for a control
- ScaleFactor: Get scale factor for each monitor (100%, 125%, etc.). If possible and OS supports DPI per monitor.