简体   繁体   English

如果我们传递对象本身,为什么我们需要EventArgs? C#

[英]Why do we need EventArgs if we passing object itself? C#

I'm still new to C# and to programming. 我还是C#和编程新手。 I already used WPF and looked for one of the socket practices. 我已经使用过WPF并寻找其中一种套接字实践。 And I just do not get the reason of existing second argument in eventHandlers. 我只是没有得到eventHandlers中现有第二个参数的原因。

I'm trying to make an async server. 我正在尝试制作异步服务器。 But the in the video the programmer have added custom eventArgs to the object itself and added them to the eventHandler too. 但是在视频中程序员已经将自定义eventArgs添加到对象本身并将它们添加到eventHandler中。 To make things a bit clearer there is the part of the code: 为了使事情更清楚,有一部分代码:

Buffer for received data class 接收数据类的缓冲区

class DataReceivedEventArgs : EventArgs
{
    public const int BufferSize = 1024;
    public byte[] BufferBytes;
    public int ToReceive;
    public MemoryStream BufferStream;

    // Constructor for this
    public DataReceivedEventArgs(int toReceive)...

    // Dispose function for this
    public void Dispose()...

    // Close function for stream
    private void Close()...
}

Client class 客户类

class Client
{
    private byte[] lenBufferBytes;
    private DataReceivedEventArgs receiveBuffer;
    private Socket socket;
    public IPEndPoint EndPoint...

    // Constructor for this
    public Client(Socket socket)
    {
        this.socket = socket;
        this.lenBufferBytes = new byte[4];
    }

    // Events
    public event EventHandler Disconnected;
    public event EventHandler<DataReceivedEventArgs> DataReceived;

    // Event Invokers
    protected virtual void OnDisconnected()
    {
        Disconnected?.Invoke(this, EventArgs.Empty);
    }

    protected virtual void OnDataReceived(DataReceivedEventArgs e)
    {
        DataReceived?.Invoke(this, e);
    }


}

So my questions are: 所以我的问题是:

1. Why do you need to pass the same thing twice? 你为什么要两次传递同样的东西?

and

2. Why would you ever need to have eventArgs if you can store everything in the object and change object class only without code breaking if need to add new args? 2.如果你可以在对象中存储所有内容并且只更改对象类而不需要代码破坏,如果需要添加新的args,为什么你需要有eventArgs?

You're not passing the same thing twice; 你不是两次传递同样的东西; the first parameter is the object that raised the event, the second details about the event. 第一个参数是引发事件的对象,第二个是有关事件的详细信息。

One reason it's a good idea to put details inside of an event object instead of just updating the sender object is because you don't know at what point the event handler is going to act on the event; 将细节放在事件对象中而不仅仅是更新发送者对象是一个好主意的一个原因是因为你不知道事件处理程序将在什么时候对事件起作用; if you just stored the event details in the sender object, then you're introducing potential race conditions. 如果您只是将事件详细信息存储在发件人对象中,那么您将引入潜在的竞争条件。 Plus, it would violate the Single Responsibility Principle and confuse two different concerns. 此外,它会违反单一责任原则并混淆两个不同的问题。

Also, in that sample, the class isn't being added in two places; 此外,在该样本中,该课程没有在两个地方添加; the client code just has a reference to an event instance , the server the definition of the event class . 客户端代码只是对事件实例的引用,服务器是事件类的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM