Anders
Anders

Reputation: 12560

How do you create an extension in C#?

I wrote this extension for stringbuilder in VB.NET:

Imports System.Runtime.CompilerServices
Public Module sbExtension
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0), ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, ByVal arg0 As Object, _
                                   ByVal arg1 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0, arg1), ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object, _
                                   ByVal arg1 As Object, _
                                   ByVal arg2 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0, arg1, arg2), ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                  ByVal format As String, _
                                  ByVal ParamArray args() As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, args), ControlChars.NewLine)
    End Sub
End Module

I tried porting it to C# (I am slowly learning/teaching myself C#), but I have been unsuccessful thus far:

using System.Runtime.CompilerServices;

namespace myExtensions
{
    public static class sbExtension
    {
        [Extension()]
        public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0)
        {
            oStr.AppendFormat("{0}{1}", string.Format(format, arg0), Environment.NewLine);
        }
        [Extension()]
        public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0, string arg1)
        {
            oStr.AppendFormat("{0}{1}", string.Format(format, arg0, arg1), Environment.NewLine);
        }
        [Extension()]
        public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string arg0, string arg1, string arg2)
        {
            oStr.AppendFormat("{0}{1}", string.Format(format, arg0, arg1, arg2), Environment.NewLine);
        }
        [Extension()]
        public static void AppendFormattedLine(this System.Text.StringBuilder oStr, string format, string[] args)
        {
            oStr.AppendFormat("{0}{1}", string.Format(format, args), Environment.NewLine);
        }
    }
}

When I have this file in my App_Code folder, I get the following error message:

Compiler Error Message: CS1112: Do not use 'System.Runtime.CompilerServices.ExtensionAttribute'. Use the 'this' keyword instead.

I am unsure of what it means by this, because if I replace '[Extension()]' with 'this', the only thing that is related to an extension is 'ExtensionAttribute', but that does not work either.

Does anyone know what I am missing?

Thanks!

Upvotes: 6

Views: 6174

Answers (5)

Hassan Faghihi
Hassan Faghihi

Reputation: 2021

the attribute you use is for extension method, which in C# you do not write it manually and the compiler do it for you, ... you can learn in here, how to write extension method in .Net using both VB & C#

http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET

http://www.codeproject.com/Articles/34295/Extend-the-NET-Library-with-Extension-Methods

Upvotes: 0

user117499
user117499

Reputation:

No one actually shows an example syntax.

public static void ApplyNewServer(this ReportDocument report, string serverName, string username, string password)
{
}

Upvotes: 13

Bela
Bela

Reputation: 3464

You'll want to change the namespace to System, along with removing the Extension attribute as others have said.

Upvotes: -1

dahlbyk
dahlbyk

Reputation: 77540

C#'s "this" keyword, which you are using correctly, is a replacement for the [Extension()] attribute. Remove those and you should be good to go.

To clarify, by "replacement" I really mean "syntactic sugar" - when the compiler sees the "this" modifier, it generates the same ExtensionAttribute you have to add by hand in VB.

Upvotes: 16

Ben M
Ben M

Reputation: 22492

Just remove the Extension attribute altogether -- C# only requires the this qualifier on the first parameter to create an extension method.

Upvotes: 11

Related Questions