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 7985347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:31:42+00:00 2026-06-04T11:31:42+00:00

My code has the following in an Action Method: catch (Exception e) { log(e);

  • 0

My code has the following in an Action Method:

    catch (Exception e)
    {
        log(e);
        return Content(ExceptionExtensions.GetFormattedErrorMessage(e));
    }

The function called looks like this:

public static class ExceptionExtensions
{
    public static string GetFormattedErrorMessage(this Exception e)

            if (e == null)
        {
            throw new ArgumentNullException("e");
        }

Can someone explain why there is a “this” at the start of the parameter list?

  • 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-06-04T11:31:43+00:00Added an answer on June 4, 2026 at 11:31 am

    This is the signature of the extension methods. They were introduced in .NET3.5(C#3). Without this the compiler would take it as a static method signature.
    in the following code:

    public class Foo
    {
        public void FooBar()
        {
    
        }
    }
    public static class FooEx
    {
        public static void Bar(this Foo f)
        {
            f.FooBar();
        }
        public static void StaticBar(Foo f)
        {
            f.FooBar();
        }
    }
    

    You can call the static method like this:

    FooEx.StaticBar(new Foo());
    

    and the extension method like this:

    new Foo().Bar();
    

    and the extension method as a static method:

    FooBar.Bar(new Foo());
    

    So, to convert an extension method to static method all you have to do is remove the this keyword from the signature.
    However, if you want to convert a static method into an extension methods you have to:

    1. Add the this keyword to the signature
    2. Put the method into a non-nested public static class
    3. Make the access modifier of the method public

    Scott Hanselman has a good article on the choice of the syntax and how it fit the existing CLR with minimal changes to the compiler.

    In summary, he concludes that after compilation, there is no difference between the generated IL code for a static method and the code for an extension method.
    The this keyword is there to instruct the compiler to put some meta data around the extension method.

    My guess is that they have gone for the this keyword because 1.it already existed and 2. its existing meaning best fitted in the context of the extension method compared to the other existing keywords.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code has the following in an Action Method: catch (Exception e) { log(e);
My routes.php has the following line of code: Router::connect('/', array('controller' => 'users', 'action' =>
Class 1 has the following code that generates the exception - -(IBAction) searchAllAction: (id)
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
In one of my Struts action I've got the following code in a method:
I've got a product page. The show action has the following code: def show
I have multiple chunks of code similar to the following: void GetPerson(Action<PersonView, Exception> callback);
The following code has concurrency issues. Here inDegVec is a global int * ,
I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap
I am generating a simple form with php. The following code has been reduced

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.