Small syntax question

  • Thread starter Thread starter Zaza
  • Start date Start date
Z

Zaza

Hi,

I'm not expert with C# (I'm more with Java and C++). I have to work
with an existing C# code. And I have a question about this lines :

...
TCP_DATAHEADER buffer = (TCP_DATAHEADER) asyncState.buffer;
Array.Copy(asyncState.buffer, buffer.cbHeader, asyncState.buffer,
0, buffer.dwBytes - buffer.cbHeader);
string dwType = buffer.dwType;
...

What does the first line do ? A simple cast or a copy ? I think it is
a copy, because the line 2 make a copy overwriting the initiale data,
and the line 3 seems to asses to this original data ...

But I'm not sure, it's strange ...

Thanks,

Xavier
 
I'm not expert with C# (I'm more with Java and C++). I have to work
with an existing C# code. And I have a question about this lines :

    ...
    TCP_DATAHEADER buffer = (TCP_DATAHEADER) asyncState.buffer;
    Array.Copy(asyncState.buffer, buffer.cbHeader, asyncState.buffer,
0, buffer.dwBytes - buffer.cbHeader);
    string dwType = buffer.dwType;
    ...

What does the first line do ? A simple cast or a copy ? I think it is
a copy, because the line 2 make a copy overwriting the initiale data,
and the line 3 seems to asses to this original data ...

We can't really tell without knowing what TCP_DATAHEADER looks like,
or what asyncState.buffer is.

Jon
 
Hi Jon,
We can't really tell without knowing what TCP_DATAHEADER looks like,
or what asyncState.buffer is.

You're right !

Oups, sorry ! I just see this line in TCP_DATAHEADER :

public static explicit operator TCP_DATAHEADER(byte[] data);

I supposed it's like in C++, and this explain the line 1 in my sample
that is not a cast but a call to this operator (that make a copy of
the data).

Thank your for your reply,

Xavier
 
We can't really tell without knowing what TCP_DATAHEADER looks like,
or what asyncState.buffer is.

You're right !

Oups, sorry ! I just see this line in TCP_DATAHEADER :

    public static explicit operator TCP_DATAHEADER(byte[] data);

I supposed it's like in C++, and this explain the line 1 in my sample
that is not a cast but a call to this operator (that make a copy of
the data).

Right. Do you have control over this type? If so, I'd get rid of the
operator and create a method FromBytes or something like that - it'll
be a lot clearer.

Jon
 
Hi Jon,

In fact, I am writing Java code for a TCP/IP protocol, and the only
documentation I have is C# code, that I have to understand.

I hav'nt to touch or change this C# code. I aggree with you, this code
is a little ambiguous ... ;-)

Thank you for your help,

Xavier
 
Back
Top