locked
What exactly is a C# Helper method? RRS feed

  • Question

  • I am taking classes on C# and mobile app development at MVA. Mr. Tabor talks about helper methods but never goes into detail about what makes a helper method any different than any other method. In the exercises it always tells us to start with making a helper method. I am not quite sure what a helper method is so I'm not sure if I am doing the exercises correctly.

    If anyone has time I am curios about what an extension method is also.

    Thursday, January 7, 2016 9:21 PM

Answers

  • Hi,

    You've ended up in the forum for the Small Basic programming language, which isn't the right place for your question. Doubtless, this thread will soon be moved to the correct section.

    ---

    A "helper method" isn't anything special, and isn't a language feature. It doesn't differ in any way from a regular method. A helper method is just a method that helps you do something else. For example, if you had to find the square root of a number multiple times within a method, you wouldn't write out the code to find the root each time you needed it, you'd separate it out into a method like Math.Sqrt.

    ---

    An extension method is a language feature. If you, for example, had a string variable and were to call one of the methods on it, you would use the variable name followed by the dot operator followed by the method name and method call operator, as below.

    string thisMonth = "January";
    
    var uppercase = thisMonth.ToUpper(); // JANUARY

    But what if you had your own method that acted on a string, like the one below?

    // Helper methods for working with strings.
    public static class StringHelpers
    {
        // Gets the first character of a string.
        public static char First(string str)
        {
            return str[0];
        }
    }
    
    string thisMonth = "January";
    var firstChar = StringHelpers.First(thisMonth); // J

    It would be pretty inconvenient if you had to call it by StringHelpers.First each time you wanted to use it, especially if you intended for other people to use your code in their projects. This is where extension methods come in. They allow you to make it so you can omit the class containing the method, and allow you to call your method directly on a variable as shown below.

    string thisMonth = "January";
    var firstChar = thisMonth.First(); // J

    How this works behind the scenes isn't very important, and you can consider it compiler magic for now. Extension methods are declared in much the same way as normal methods, just with the this keyword before the first argument (and the first argument being of the type you want to be able to call the method on).

    public static class StringHelpers
    {
        public static char First(this string str)
        {
            return str[0];
        }
    }
    
    string thisMonth = "January";
    var firstChar_0 = thisMonth.First(); // J
    var firstChar_1 = StringHelpers.First(thisMonth); // J
    
    Console.WriteLine(firstChar_0 == firstChar_1); // True

    You might be unsure what use this is (I was when I first came across them). Probably the most common use is with LINQ, which uses extension methods to add extra functionality to any class that implements IEnumerable<T>. Through LINQ's use of extension methods, you can use methods that work on List<T>s, arrays, and anything else that implements IEnumerable<T> even though the method is only defined in one place (which makes the method easier to maintain).



    • Proposed as answer by Nonki Takahashi Thursday, January 7, 2016 11:27 PM
    • Edited by Liam McSherry Friday, January 8, 2016 1:01 PM
    • Marked as answer by litdev Saturday, January 16, 2016 5:03 PM
    Thursday, January 7, 2016 10:33 PM