steelbull
steelbull

Reputation: 141

C# Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

I have following methods for serial communication:

private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  int rxLength = this._serialPort.BytesToRead;
  byte[] rxBuf = new byte[rxLength];
  try
  {
    rxLength = this._serialPort.Read(rxBuf, 0, rxLength);
    this.BeginInvoke((Delegate) (() =>
    {
      this._dataRx.AddBytes(rxBuf, 0, rxLength);
      this.AddDataToAscii(rxBuf, rxLength, true);
    }));
  }
  catch (Exception ex)
  {
  }
}

...and

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Delegate) (() => { AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}

Both of this methods returns following error: Severity Code Description Project File Line Suppression State Error CS1660 Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

how can I modify the source to define missing delegate type?

Thx.

Upvotes: 1

Views: 6953

Answers (1)

Antoine V
Antoine V

Reputation: 7204

Action is a Type of Delegate provided by the .NET framework. The Action points to a method with no parameters and does not return a value.

In your case, to make working your code, replace with Action:

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Action) (() => { 
      AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}

Upvotes: 2

Related Questions