Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:44:33+00:00 2026-05-15T08:44:33+00:00

Looking at some code examples for HtmlHelpers , and I see declarations that look

  • 0

Looking at some code examples for HtmlHelpers, and I see declarations that look like this:

public static string HelperName(this HtmlHelper htmlHelper, ...more regular params )

I can’t remember seeing this type of construct anywhere else – can someone explain the purpose of the "this"? I thought that by declaring something public static meant that the class did not need to be instantiated – so what is "this" in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:44:34+00:00Added an answer on May 15, 2026 at 8:44 am

    This is the syntax for declaring extension methods, a new feature of C# 3.0.

    An extension method is part code, part compiler “magic”, where the compiler with the help of intellisense in Visual Studio make it appear that your extension method is actually available as an instance method on the object in question.

    Let me give an example.

    There’s no method on the String class that is named GobbleGobble, so let’s create an extension method:

    public static class StringExtensions
    {
        public static void GobbleGobble(this string s)
        {
            Console.Out.WriteLine("Gobble Gobble, " + s);
        }
    }
    

    The class name is just my naming convention, it isn’t necessary to name it like that, but it has to be static, as do the method.

    After declaring the above method, you can, in Visual Studio, type this:

    String s = "Turkey Baster!";
    s.
    

    after the dot, wait for intellisense, and notice there is a GobbleGobble method there, complete the code like this:

    String s = "Turkey Baster!";
    s.GobbleGobble();
    

    Important: The class where the extension method is declared must be available to the compiler and the intellisense processor in order for intellisense to show the method. If you type in GobbleGobble manually, and use the Ctrl+. shortcut, it will not help you get the right using directives into the file.

    Notice that the parameter to the method has disappeared. The compiler will silently move around the important bits, which are:

    String s = "Turkey Baster!";
    s.GobbleGobble();
    ^     ^
    |     +-- the compiler will find this in the StringExtensions class
    |
    +-- will be used as the first parameter to the method
    

    Thus, the above code will be transformed by the compiler to this:

    String s = "Turkey Baster!";
    StringExtensions.GobbleGobble(s);
    

    So at call-time, there’s nothing magical about it, it’s just a call to a static method.

    Note that if your extension method declares more than one parameter, only the first supports the this modifier, and the rest has to be specified as part of the method call as normal:

    public static void GobbleGobble(this string value, string extra)
    {                                            |              |
        ...                                      |              |
    }                                            |              |
                                                 |              |
    +--------------------------------------------+              |
    |                                                           |
    v                                                           |
    s.GobbleGobble("extra goes here");                          |
                            ^                                   |
                            |                                   |
                            +-----------------------------------+
    

    Extension methods was added in part due to Linq, where the Linq syntax of C# will look for appropriately named extension methods for the objects in play, which means you can “introduce” Linq-support into any type of class by just declaring the right extension methods. Of course, full Linq support is a lot of work, but it is possible.

    Also, extension methods by themselves are really useful, so read up on it.

    Here’s a few links:

    • MSDN: Extension Methods (C# Programming Guide)
    • MSDN Magazine – Basic Instincts: Extension Methods
    • Wikipedia: Extension Method
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There isn't a way to do this. If a variable… May 15, 2026 at 7:34 pm
  • Editorial Team
    Editorial Team added an answer Standard solution is to use std::numeric_limits. For instance, std::numeric_limits<long>::max(). You… May 15, 2026 at 7:34 pm
  • Editorial Team
    Editorial Team added an answer You really are better off using a database for this… May 15, 2026 at 7:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.