Small syntax question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zaza

    Small syntax question

    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.buff er;
    Array.Copy(asyn cState.buffer, buffer.cbHeader , asyncState.buff er,
    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

  • Jon Skeet [C# MVP]

    #2
    Re: Small syntax question

    On Sep 24, 9:04 am, Zaza <xno...@hotmail .comwrote:
    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.buff er;
        Array.Copy(asyn cState.buffer, buffer.cbHeader , asyncState.buff er,
    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.buff er is.

    Jon

    Comment

    • Zaza

      #3
      Re: Small syntax question

      Hi Jon,
      We can't really tell without knowing what TCP_DATAHEADER looks like,
      or what asyncState.buff er 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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Small syntax question

        On Sep 24, 2:03 pm, Zaza <xno...@hotmail .comwrote:
        We can't really tell without knowing what TCP_DATAHEADER looks like,
        or what asyncState.buff er 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

        Comment

        • Zaza

          #5
          Re: Small syntax question

          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

          Comment

          Working...