I am trying to get this method into my project how do i do it http://extensionmethod.net/1718/csharp/string/leftof
Asked
Active
Viewed 59 times
-2
-
1The 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
-
1Copy 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 Answers
-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
-
-
I think you need to go and read up on what an [extension method](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) is. – DavidG Nov 07 '18 at 23:24
-
-
Hope that is better Agent Magma, there was two examples I retracted my last because I got flamed. – Levon Ravel Nov 07 '18 at 23:36
-
You didn't get flamed, you got told you were wrong, there's a very big difference. Secondly, adding links to code is highly discouraged here, you have made your answer worse. – DavidG Nov 07 '18 at 23:37
-
-