我還是C#和編程的新手。我已經使用了WPF並尋找了一個socket實踐。我只是沒有得到eventHandlers中存在的第二個參數的原因。如果我們傳遞對象本身,爲什麼我們需要EventArgs? C#
我試圖做一個異步服務器。但是在視頻中,程序員已經將自定義eventArgs添加到對象本身,並將它們添加到eventHandler中。爲了讓事情更清楚一點有代碼的一部分:
緩衝器,用於接收到的數據類
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 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);
}
}
所以我的問題是:
1.爲什麼你需要兩次傳遞相同的東西?
和
2.爲什麼你需要有EventArgs的,如果你能在對象存儲一切信息並更改對象類唯一沒有密碼破譯,如果需要添加新的ARGS?
好耶,這讓更多的現在感覺到了一點,但如果我再丟失了,我會再回到這個問題。無論如何,幫助Thx :) –