Understanding C# Events use of sender object

后端 未结 5 1051
时光说笑
时光说笑 2020-12-09 14:07

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.

相关标签:
5条回答
  • 2020-12-09 14:37

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

    You would get a reference to your object.

    0 讨论(0)
  • 2020-12-09 14:41

    Just a note: If you have multiple different controls leading to the same method you can use

    ((Control)sender)
    

    to access it for every control, regardless the type of control (above it was just hardwritten, what object it has to be)

    0 讨论(0)
  • 2020-12-09 14:50

    In .NET, all classes are reference types, and a reference is always passed... by reference (the current implementation of a reference is a pointer that can be moved around by the GC when it needs to), so you don't have to worry about anything.

    About events, the sender parameter is always the object that generated the event (for example a Button in a Click event on a button). If you want to pass arbitrary data in a custom event, inherits from EventArgs and pass it as the second argument.

    0 讨论(0)
  • 2020-12-09 14:56

    When parameters are passed by reference,

    1. The properties present inside the reference instance can be change without affecting the original reference instance.

    2. Original reference can be changed using the ref keyword.

    The following is an example

    public partial class ParentForm : Form
    {
        public ParentForm()
        {
            InitializeComponent();
            ChildForm childForm = new ChildForm();
            ChangeCaption( childForm );
            //Will display the dialog with the changed caption.
            childForm.ShowDialog();
            ChangeCaption( ref childForm );
            //Will give error as it is null
            childForm.ShowDialog();
        }
    
        void ChangeCaption( ChildForm formParam )
        {
            // This will change openForm’s display text
            formParam.Text = "From name has now been changed";
            // No effect on openForm
            formParam = null;                        
        }
    
    
        void ChangeCaption( ref ChildForm formParam )
        {
            // This will change openForm’s display text
            formParam.Text = "From name has now been changed";
            // This will destroy the openForm variable!
            formParam = null;                        
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:00

    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.

    0 讨论(0)
提交回复
热议问题