-2

I am trying to get this method into my project how do i do it http://extensionmethod.net/1718/csharp/string/leftof

Agent Magma
  • 41
  • 1
  • 7
  • 1
    The simplest way, albeit not always the best, to get code in your application, is to copy and paste it. Really, the link is not a package nor a DLL nor anything strange, just plain code. Copy and paste in a class on your project. – Camilo Terevinto Nov 07 '18 at 23:09
  • 1
    Copy and pasting a bit of code without understanding how that code works and what are the requirements to use it is not of much usefulness. The OP should start to read how to write an extension method and in particular about the requirement to have a static class. – Steve Nov 07 '18 at 23:18
  • Thank you for helping me with this – Agent Magma Nov 07 '18 at 23:32

1 Answers1

-1

Here is a extension example per your request. The extenstion is created via the Extensions class you can put many extensions in here. The way the extension is initially created is the first parameter would be the type you want to extend by using "this" modifier and then your Type. Any other parameters following can be utilized to perform your logic. From here any time you utilize the Type you can see the extension IE myString.LeftOf('somecharacter');

using System;
namespace YourNameSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "Hello World";
            char character = 'l';
            string result = myString.LeftOf(character);
            Console.WriteLine(result);
        }       
    }

    public static class Extensions
    {
        public static string LeftOf(this string s, char c)
        {
            int ndx = s.IndexOf(c);
            if (ndx >= 0)
            {
                return s.Substring(0, ndx);
            }

            return s;
        }
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Levon Ravel
  • 90
  • 1
  • 10