You must Sign In to post a response.
  • Category: .NET

    How to implement typedef struct union in C#

    Hi guys,

    I have to rewrite this C++ code in C#. C# does not support typedef or union. How can I do this in C# ?

    typedef enum {
    TYPE_FLOAT, TYPE_STRING,TYPE_INTEGER, TYPE_DATETIME
    } tdFuncType;

    typedef struct {
    char szNm[256]; // Identifier name, if not constant or computed
    tdFuncType ft;
    union {
    double dFloat; // TYPE_FLOAT
    long lInteger; // TYPE_INTEGER
    time_t tDateTime; // TYPE_DATETIME
    char szString[256]; // TYPE_STRING, max string size is 255
    } val;
    } tdFuncValue;

    // All values
    typedef struct {
    long lCt; // Number of actual values: 1 to 11
    tdFuncValue fv[11]; // Values
    } tdFuncValues;

    Any ideas ?

    Thanks.

    Pr.
  • #766509
    Hi,
    In C# you can use 'StructLayout(LayoutKind.Explicit)' and 'FieldOffset' attributes to create equivalent functionality to UNION in C.
    'StructLayout(LayoutKind.Explicit)' allows us to control the physical layout of data fields of a class/structure in memory. It is available in library 'System.Runtime.InteropServices'.
    Please go through this URL for more detailed information and source code:
    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx
    Also refer this:
    https://social.msdn.microsoft.com/Forums/en-US/019a258e-8d50-4a9f-b0ef-8311208ebb6a/typedefequivalent-in-c?forum=csharplanguage

  • #766600
    No, there's no equivalent of typedef in C# you can use 'using ' directives instead, see below snippet

    using CustomerList = System.Collections.Generic.List<Customer>;

    If you need it across multiple files then a simple struct as a wrapper will do the job.
    1) Even with C++, you do need keyword "typedef" to declare structure or class. I think this is C anachronism.
    2) C# structure is semantically the closest to C++; with classes, for example, there are much more differences. C# structure carry value semantic, but C# classes -- reference semantics and a lot more differences.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766617
    In C#, you need to use either structures or classes. If you have reference types then go for classes else go for structures.
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #766638
    Hi,

    Go through the below mentioned Links, It will help you.

    1.http://stackoverflow.com/questions/5630149/how-to-convert-a-c-struct-with-union-into-c

    2.http://stackoverflow.com/questions/8006965/how-use-struct-with-union-in-c-sharp

    3.http://www.codeproject.com/Questions/141385/typedef-in-C

  • #766885
    Please find the below links ....

    1.http://stackoverflow.com/questions/5630149/how-to-convert-a-c-struct-with-union-into-c

    2.http://stackoverflow.com/questions/8006965/how-use-struct-with-union-in-c-sharp

    3.http://www.c-sharpcorner.com/article/using-pointers-in-C-Sharp/

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


Sign In to post your comments