Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this extension method to force a string into an integer or default to 0 redundant?

I needed a one-liner to convert a string to an integer, and if the string is anything invalid, then just set it to zero. So I made the following extension method, but I can imagine there is a system method to do this in one line.

Is there another way to do this in one line without creating an extension method?

using System;

namespace TestForceInteger
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("testing".ForceInteger() + 1);
            Console.WriteLine("199".ForceInteger() + 1);
            int test = StringHelpers.ForceInteger(null);
            Console.WriteLine(test + 1);
            Console.ReadLine();
            //output:
            //1
            //200
            //1
        }
    }

    public static class StringHelpers
    {
        public static int ForceInteger(this string potentialInteger)
        {
            int result;
            if (int.TryParse(potentialInteger, out result))
                return result;
            else
                return 0;
        }
    }
}
like image 862
Edward Tanguay Avatar asked Aug 30 '10 14:08

Edward Tanguay


People also ask

What is the correct definition of extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

How do you add an extension to an integer?

So the first parameter must be int preceded with the this modifier. For example, the IsGreaterThan() method operates on int, so the first parameter would be, this int i . Now, you can include the ExtensionMethods namespace wherever you want to use this extension method.

What is the difference between a static method and an extension method?

An extension method is "a public static method in a static class" and can still be used as such. It just has the additional syntactic sugar of being callable with a different syntax.

What is extension method in MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.


2 Answers

Not as a one-liner, afaik, but if you want to avoid the extension method you can initialize the result to 0 and ignore the outcome of TryParse:

int result = 0;
int.TryParse(potentialInteger, out result)
like image 73
Paolo Tedesco Avatar answered Sep 29 '22 07:09

Paolo Tedesco


Not a conventional way with the framework libraries, no.

Your extension method would be better if it returned a nullable int, so that you could distinguish between "invalid" and 0.

like image 31
mqp Avatar answered Sep 29 '22 08:09

mqp