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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:48:37+00:00 2026-05-24T12:48:37+00:00

When I write an event handler for csharp, it looks like this: public void

  • 0

When I write an event handler for csharp, it looks like this:

public void FooHandler(object sender, EventArgs e)
{
    //do stuff..
    this.doSomething();  //Does the "this" keyword mean something in this context?
}

Does the “this” keyword mean something in this context?

EDIT:

Let’s say I also have this code:

public class GizmoManager {
    public void Manage() {
        g = new Gizmo();
        g.Foo += new EventHandler(FooHandler);
    }
}

What would the this (within FooHandler) refer to?

  • 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-24T12:48:37+00:00Added an answer on May 24, 2026 at 12:48 pm

    Yes, it’s a reference to object for which FooHandler() is called. Delegates are capable of referencing both static and non-static methods. When talking about non-static ones, this is a reference to object instance.

    class A
    {
       public delegate void MyDelegate(object sender, int x);
       public event MyDelegate TheEvent;
    
       public void func()
       {
         if(TheEvent != null) TheEvent(this, 123);
       }
    }
    
    class B
    {
       public B()
       {
         A a = new A();
         a.TheEvent += handler;
         a.func();
       }
    
       public void handler(object sender, int x)
       {
          // "sender" is a reference to object of type A that we've created in ctor
          // "x" is 123
          // "this" is a reference to B (b below)
       } 
    }
    
    B b = new B(); // here it starts
    

    Some more details. Your code:

    g = new Gizmo();
    g.Foo += new EventHandler(FooHandler);
    

    could be re-written like this

    g = new Gizmo();
    g.Foo += new EventHandler(this.FooHandler); // look here
    

    In this case this is the same this that you have in your handler 😉

    And even more, if you have some problems with understanding this:

    class X
    {
      int a;
    
      public X(int b)
      {
        this.a = b; // this stands for "this object"
        // a = b is absolutely the same
      }
    
      public X getItsThis()
      {
        return this;
      }
    }
    
    X x = new X();
    X x2 = x.getItsThis();
    // x and x2 refer to THE SAME object
    // there's still only one object of class X, but 2 references: x and x2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
I'm trying to write an event handler that fires every time a node in
Is it possible in VB.NET to easily write an event handler that will handle
In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler
In an attempt to create cleaner code, I am trying to write event handler
How to write '{' in inline event handler in the mxml tag itself? Suppose
I added an event handler to redirect error_logger reports to syslog. The handler looks
How can I write from Java to the Windows Event Log?
I am trying to use the Event Log to write some debug information and
How can I get an application to write debug text to the Event Log

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.