CSharp - Create extension for string to convert string to double and bool

Introduction

Both the class and every method it contains are static.

Extension methods must be defined in a non-generic static class

Demo

using System;
using System.Linq;
class Program//from   w  w w  . j a v  a2  s  .c  o m
{
    static void Main(string[] args)
    {
        double pi = "3.1415926535".ToDouble();
        Console.WriteLine(pi);
    }
}
public static class StringConversions
{
    public static double ToDouble(this string s)
    {
        return Double.Parse(s);
    }

    public static bool ToBool(this string s)
    {
        return Boolean.Parse(s);
    }
}

Result

Related Topic