<p>How is it possible to make prototype methods in C#.Net?</p>

<p>In JavaScript, I can do the following to create a trim method for the string object:</p>

<pre><code>String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
</code></pre>

<p>How can I go about doing this in C#.Net?</p>

[{'answer_id': 4614, 'author': 'David Wengier', 'author_id': 489, 'author_profile': 'https://Stackoverflow.com/users/489', 'pm_score': 0, 'selected': False, 'text': '<p>You need to create an extension method, which requires .NET 3.5. The method needs to be static, in a static class. The first parameter of the method needs to be prefixed with "this" in the signature.</p>\n\n<pre><code>public static string MyMethod(this string input)\n{\n    // do things\n}\n</code></pre>\n\n<p>You can then call it like</p>\n\n<pre><code>"asdfas".MyMethod();\n</code></pre>\n'}, {'answer_id': 4615, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 6, 'selected': True, 'text': '<p>You can\'t dynamically add methods to existing objects or classes in .NET, except by changing the source for that class.</p>\n\n<p>You can, however, in C# 3.0, use extension methods, which <em>look</em> like new methods, but are compile-time magic.</p>\n\n<p>To do this for your code:</p>\n\n<pre><code>public static class StringExtensions\n{\n    public static String trim(this String s)\n    {\n        return s.Trim();\n    }\n}\n</code></pre>\n\n<p>To use it:</p>\n\n<pre><code>String s = "  Test  ";\ns = s.trim();\n</code></pre>\n\n<p>This looks like a new method, but will compile the exact same way as this code:</p>\n\n<pre><code>String s = "  Test  ";\ns = StringExtensions.trim(s);\n</code></pre>\n\n<p>What exactly are you trying to accomplish? Perhaps there are better ways of doing what you want?</p>\n'}, {'answer_id': 4616, 'author': 'Andrew Peters', 'author_id': 608, 'author_profile': 'https://Stackoverflow.com/users/608', 'pm_score': 0, 'selected': False, 'text': '<p>Using the 3.5 compiler you can use an Extension Method:</p>\n\n<pre><code>public static void Trim(this string s)\n{\n  // implementation\n}\n</code></pre>\n\n<p>You can use this on a CLR 2.0 targeted project (3.5 compiler) by including this hack:</p>\n\n<pre><code>namespace System.Runtime.CompilerServices\n{\n  [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]\n  public sealed class ExtensionAttribute : Attribute\n  {\n  }\n}\n</code></pre>\n'}, {'answer_id': 4619, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 3, 'selected': False, 'text': '<p>It sounds like you\'re talking about C#\'s Extension Methods. You add functionality to existing classes by inserting the "this" keyword before the first parameter. The method has to be a static method in a static class. Strings in .NET already have a "Trim" method, so I\'ll use another example.</p>\n\n<pre><code>public static class MyStringEtensions\n{\n    public static bool ContainsMabster(this string s)\n    {\n        return s.Contains("Mabster");\n    }\n}\n</code></pre>\n\n<p>So now every string has a tremendously useful ContainsMabster method, which I can use like this:</p>\n\n<pre><code>if ("Why hello there, Mabster!".ContainsMabster()) { /* ... */ }\n</code></pre>\n\n<p>Note that you can also add extension methods to interfaces (eg IList), which means that any class implementing that interface will also pick up that new method.</p>\n\n<p>Any extra parameters you declare in the extension method (after the first "this" parameter) are treated as normal parameters.</p>\n'}]
['https://Stackoverflow.com/questions/4610', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']