Skip to content

Provides a Parse generic extension method for string that converts string to T and no boxing occurs.

License

Notifications You must be signed in to change notification settings

vst-h/StringParse

Repository files navigation

StringParse

NuGet Version NuGet Download

Provides a Parse generic extension method for string that converts string to T and no boxing occurs.

// The following code does not occur boxing
var a1 = "123".Parse<int>();        // a1 == 123
var a3 = "123".Parse<int?>();       // a3 == 123

Usage example

using System;
using System.Globalization;
using StringParse;

void Exmaple1() {
    _ = "12".Parse<int>();      // 12
    // _ = "ss".Parse<int>();      // throw FormatException

    // with default value, Actually call the TryParse method internally
    _ = "12".ParseOr(10);     // 12
    _ = "ss".ParseOr(10);     // 10

    // with NumberStyles
    _ = "F".Parse<int>(NumberStyles.HexNumber);      // 15

    // parse Nullable
    _ = "12".Parse<int?>();      // 12
    // _ = "ss".Parse<int?>();      // throw FormatException

    // TryParse
    _ = "12".TryParse(out int _);      // true
    _ = "ss".TryParse(out int _);      // false

    // Parse Enum
    _ = "Saturday".ParseEnum<DayOfWeek>();          // DayOfWeek.Saturday
    _ = "Saturday".TryParseEnum(out DayOfWeek _);   // true
}

About

Provides a Parse generic extension method for string that converts string to T and no boxing occurs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages