1

The following function takes an object as a parameter, the object is actually a tuple and is cast as such:

private void MyFunction(object clientWithThreadIDAsObject)
{
    (TcpClient m_sender, int m_nListenerThreadID) clientWithThreadID = ((TcpClient m_sender, int m_nListenerThreadID))clientWithThreadIDAsObject;

Here is the function being called from elsewhere in the code:

(TcpClient m_sender, int m_nListenerThreadID) ClientWithThreadID = (sender, m_nNextListenerThreadID);
                    
MyFunction(ClientWithThreadID);

So the text (TcpClient m_sender, int m_nListenerThreadID) has been written three times.

Is it possible to somehow define a shorthand for this? (Like a macro in c).

Some way to not have to type (TcpClient m_sender, int m_nListenerThreadID) every time it is needed?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
gnitsuk
  • 31
  • 3
  • You can define a class encapsulating the tuple and pass it as the parameter – Marco Beninca Jul 15 '22 at 09:48
  • 1
    Best bet here is to define a record: `public sealed record TcpTransfer(TcpClient Sender, int ListenerId);` and use that instead of a tuple. For such a small type you might even want to make it a readonly record struct: `public readonly record struct TcpTransfer(TcpClient Sender, int ListenerId);` – Matthew Watson Jul 15 '22 at 09:50
  • 3
    In two of the three places, it was not actually needed: you could just use `var` instead. Only the cast requires writing the tuple definition. – M Kloster Jul 15 '22 at 09:51
  • 3
    Why this method takes `object` if it immediately casts it to that tuple? – Tim Schmelter Jul 15 '22 at 09:52
  • A small demonstration of M Kloster's comment: [fiddle](https://dotnetfiddle.net/fVIdEN) – Astrid E. Oct 05 '22 at 15:04

0 Answers0