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

  • SEARCH
  • Home
  • 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 6676227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:57:39+00:00 2026-05-26T03:57:39+00:00

I was wondering about the following: private void RandomEventHandler_Click(object sender, EventArgs e) { //

  • 0

I was wondering about the following:

private void RandomEventHandler_Click(object sender, EventArgs e)
{
// SOME CODE IN EVENTHANDLER HERE
}

Button BtnPushed = (Button)sender;

How come it’s possible to cast from an object in the eventhandler to for example a Button, but without having to create an object of the class Button first?

For example like this:

private void RandomEventHandler_Click(object sender, EventArgs e)
{
// SOME CODE IN EVENTHANDLER HERE
}

Button BtnPushed = new Button();

BtnPushed = (Button)sender;

Normally you would have to create an object of a class, which is a reference type before you can work with it.

Please explain. Thx!

  • 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-26T03:57:40+00:00Added an answer on May 26, 2026 at 3:57 am

    I think you might be mixing up things. From the examples you gave, I guess you might be confused by event handlers, and the fact that they usually have a method signature along the lines of
    void Handler(object sender, EventArgs args) where sender might be an instance of say a Button.

    In OOP types can inherit from other types and you can use instances of inherited types as instances of base types, this concept is called Polymorphism. For instance, the Button class inherits from Object, somewhere up the inheritance chain. You could say that an inherited type is a specialized kind of its base type:

    class Person { } // Person is our base type
    class John : Person {} // John is a specialized kind of Person
    

    Therefor you could say that when creating an instance of John, that this is also an instance of Person:

    John john = new John();
    Person johnAsPerson = john; // Perfectly fine, makes sense even
    

    But not the other way around, because not all people are John, some might be Steve or Mary, some might even just be Person:

    Person person = new Person();
    John john = person; // Won't work 
    

    In those cases where we know that it’s an instance of John, we can tell the compiler “don’t worry, I know what I’m doing”, and explicitly cast the Person to a John:

    Foo(new John());
    
    static void Foo(Person person) {
        John john = (John)person; // We expect it to always be a instance of John
        // If not, the runtime will throw an exception, that we're trying to do an invalid cast
    }
    

    To be clear, it’s important to differentiate between the Type of an instance, and the Type of a variable:

    string a = "123";
    //     |    |-> I'm an instance of the Type `string`
    //     |-> I'm a variable of type `string`, that points to an instance of type `string`
    object o = a;
    //     |-> I'm a variable of type `object`, that points to an instance of type `string`
    

    The point with event handlers is that they usually provide the sending control instance in the form of its base type Object. The instance of the control is still very much a Button, but we’re just looking at it as an Object. Therefor you can cast the Object to a Button.

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

Sidebar

Related Questions

I've been wondering about how hard it would be to write some Python code
I am wondering about the the following piece of Java code: System.out.println. I am
I have some questions about the following code: using System; namespace ConsoleApplication2 { public
I am wondering about the following C#-code: struct Structure { public Structure(int a, int
Reading this DZone article about Java concurrency I was wondering if the following code:
So, I was wondering about the following: I have some main content div and
I'm wondering how i would go about making the following application: a user signs
Following on from my other post about primary keys I am wondering if there
I was wondering, whether the following code are safe. public class GUIBundle { //
I'm playing around with WPF and databinding and I'm wondering about the following. I

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.