tags:

views:

1206

answers:

6

How can I make prototype methods in C#.Net?

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

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

How can I go about doing this in C#.Net

A: 

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.

public static string MyMethod(this string input)
{
// do things
}

You can then call it like

"asdfas".MyMethod();
Ch00k
+10  A: 

You can't dynamically add methods to existing objects or classes in .NET, except by changing the source for that class.

You can, however, in C# 3.0, use extension methods, which look like new methods, but are compile-time magic.

To do this for your code:

public static class StringExtensions
{
public static String trim(this String s)
{
return s.Trim();
}
}

To use it:

String s = "  Test  ";
s = s.trim();

This looks like a new method, but will compile the exact same way as this code:

String s = "  Test  ";
s = StringExtensions.trim(s);

What exactly are you trying to accomplish? Perhaps there are better ways of doing what you want?

Lasse V. Karlsen
A: 

Using the 3.5 compiler you can use an Extension Method:

public static void Trim(this string s)
{
// implementation
}

You can use this on a CLR 2.0 targeted project (3.5 compiler) by including this hack:

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}
Andrew Peters
+1  A: 

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.

public static class MyStringEtensions
{
    public static bool ContainsMabster(this string s)
    {
        return s.Contains("Mabster");
    }
}

So now every string has a tremendously useful ContainsMabster method, which I can use like this:

if ("Why hello there, Mabster!".ContainsMabster()) { /* ... */ }

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.

Any extra parameters you declare in the extension method (after the first "this" parameter) are treated as normal parameters.

Matt Hamilton
A: 

Thanks Lassevk, great answer :) In response to "What exactly are you trying to accomplish?".

Every now and again, I have the need to manipulate a string or other object. Instead of having to call a function todo this, I would have thought it better to call it as a method.

I'm currently writting web applications in Asp.net and I don't think there Asp.net 3.x yet, so I'll have to wait for now. But thanks for your answer.

GateKiller
A: 

I have given this a go in ASP.NET now running 3.5 by adding the following to my code:

<script runat="server">

public static class StringExtensions
{
    public static String Trim(this String s)
    {
        return s.Trim();
    }
}


</script>

However, I get the following error message:

CS1109: Extension methods must be defined in a top level static class; StringExtensions is a nested class

Can anyone help further?

GateKiller
I can only assume that the declaration you have above is implicitly nested within the page's class. Try moving your extension to a separate C# code file.
smaclell