Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding C# Events use of sender object

I am reasonably new to C# as a language (coming from a C++ background) and I am currently in the process of writing an application that makes use of an event driven API.

Primarily this consists of registering event/response handlers and starting event monitors then dealing with these asychronous events/responses.

The thing i'm having a bit of trouble understanding is the use of the sender object.

What I would like to use it for is to pass a handle to a class object I have with various structures and data in when making a request (or setting up a monitor). And then on the response being recieved/the event being raised I can take the sender object, cast it back to the expected class type and access members, make further changes etc. so treating it as if it's just still a pointer to the original data (which I'm hoping it would be?).

So my question really is, as I am passing a class object in my request, will this be effectively a reference, or will it be copied somewhere along the line by value as it is actually just a generic object and I will end up with an empty copy of my class object on the event?

Or the third option that i'm maybe completely on the wrong track here and should forget the whole thing? :)

Problem is my brains still working in pointers mode I think...

like image 545
Adam Cobb Avatar asked Oct 07 '09 10:10

Adam Cobb


People also ask

What are the basics for C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.

How do you explain C?

C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).

Is C easy to understand?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.

Can I learn C on my own?

To get started with C or C++, you will want a compiler—although nowadays you can also learn C online by experimenting with “hello world” C projects in-browser. Compilers are programs that can be run through command-line interfaces (CLIs).


2 Answers

It's a reference. Try out this code to see how it works:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    Whatever(sender);
}

private void Whatever(object sender)
{
    TextBox tb = (TextBox)sender;
    tb.Text = "yo";
}

If object wasn't passed by reference, textBox1 would retain whatever text you typed into it.

like image 161
MusiGenesis Avatar answered Sep 20 '22 21:09

MusiGenesis


I don't know that I entirely understand your question. But to answer you in part:

You would get a reference to your object.

like image 32
Geoff Avatar answered Sep 21 '22 21:09

Geoff