ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToLong

Converts a string into a "Long", if invalid returns 0

Source

/// <summary>
/// Returns long value from string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static long ToLong(this string value)
{
    long result = 0;
    long.TryParse(value, out result);
    return result;
}

Example

public static void Main()
{      
     Console.WriteLine("1175814".ToLong()); // returns 1175814
     Console.WriteLine("invalidLong".ToLong()); // returns 0
}

Author: Luiz Fernando Corrêa Leite

Submitted on: 28 jan. 2020

Language: csharp

Type: System.String

Views: 3424