Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array question

Array question

Scheduled Pinned Locked Moved C#
questioncsharpc++data-structures
7 Posts 4 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Woodbury
    wrote on last edited by
    #1

    Is there any equivilent in C# of the following C++ construct:

    struct Data
    {
       int val;
       char* pStr;
    };
    
    Data data[] = 
    {
       { 0, "Zero" },
       { 1, "One" },
       { -1, NULL }
    };
    

    I tried the following, and a few variations, but they didn't work:

    public struct Data
    {
       public int val;
       public string str;
    };
    
    static public Data data[] = 
    {
       { 0, "Zero" },
       { 1, "One" },
       { -1, NULL }
    };
    

    The following works, but it really bugs me:

    public struct Data
    {
       public int val;
       public string str;
       public Data(int v, string s) { val = v; str = s; }
    };
    
    static public Data data[] = 
    {
       new Data(0, "Zero"),
       new Data(1, "One" ),
       new Data(-1, NULL )
    };
    

    Am I missing something? Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

    J 1 Reply Last reply
    0
    • J Joe Woodbury

      Is there any equivilent in C# of the following C++ construct:

      struct Data
      {
         int val;
         char* pStr;
      };
      
      Data data[] = 
      {
         { 0, "Zero" },
         { 1, "One" },
         { -1, NULL }
      };
      

      I tried the following, and a few variations, but they didn't work:

      public struct Data
      {
         public int val;
         public string str;
      };
      
      static public Data data[] = 
      {
         { 0, "Zero" },
         { 1, "One" },
         { -1, NULL }
      };
      

      The following works, but it really bugs me:

      public struct Data
      {
         public int val;
         public string str;
         public Data(int v, string s) { val = v; str = s; }
      };
      
      static public Data data[] = 
      {
         new Data(0, "Zero"),
         new Data(1, "One" ),
         new Data(-1, NULL )
      };
      

      Am I missing something? Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

      J Offline
      J Offline
      jparsons
      wrote on last edited by
      #2

      What bothers you about this? Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

      J 1 Reply Last reply
      0
      • J jparsons

        What bothers you about this? Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        jparsons wrote: What bothers you about this? For years I programmed in assembly. Ever since part of my brain is always thinking about what is really happening "underneath." I'm not well versed with how ILASM and how .NET actually does certain things. The construct I used looks inefficient, even though it very well may be. Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

        F J 2 Replies Last reply
        0
        • J Joe Woodbury

          jparsons wrote: What bothers you about this? For years I programmed in assembly. Ever since part of my brain is always thinking about what is really happening "underneath." I'm not well versed with how ILASM and how .NET actually does certain things. The construct I used looks inefficient, even though it very well may be. Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

          F Offline
          F Offline
          Frank Olorin Rizzi
          wrote on last edited by
          #4

          Well... static public Data data[] = { new Data(0, "Zero"), new Data(1, "One" ), new Data(-1, NULL )}; In a sense, it's the only logical thing to do (and allow).. After all, in C# arrays are actually a type of their own (unlike the older C style case where an array is just a pointer). So, while the first line begins the instanciation of a new Array of Data, you still have to instanciate and initialize each data (hence the other lines look like they do). The longer version of the same code is (forgetting about static and public): Data[] data = new Data[3]; data[0] = new Data(0, "Zero"); data[1] = new Data(1, "One"); data[2] = new Data(-1, NULL); Until the second line, data[0] has not been instanciated to be a Data object (yet, data[] has been instanciated to be an array of Data objects that can accomodate 3 Data instances). Might seem inefficient, but, thinking of the Data instances as objects of their own right, it makes sense (sort-of)... ..sure, in your case they are structs, but I think that at this point they just figured it was better to avoid a different syntax for structs vs classes... I saw this situation in Java the first time, and it took a bit to get used to it (yeah, I kept on thinking that after Data[] data = new Data[3] all three data objects where instanciated, pointing to NULL... again, they are not pointers!). F.O.R.

          1 Reply Last reply
          0
          • J Joe Woodbury

            jparsons wrote: What bothers you about this? For years I programmed in assembly. Ever since part of my brain is always thinking about what is really happening "underneath." I'm not well versed with how ILASM and how .NET actually does certain things. The construct I used looks inefficient, even though it very well may be. Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

            J Offline
            J Offline
            jparsons
            wrote on last edited by
            #5

            I'm actually not sure what happens in this case. Your data type is a struct so it's a value type. Typically value types are allocated on the stack instead of the heap. However, you're array is most likely (please correct me if I'm wrong here) is a reference type and will be allocated on the heap. So I'm not sure exactly where the value types memory will be allocated. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

            J 1 Reply Last reply
            0
            • J jparsons

              I'm actually not sure what happens in this case. Your data type is a struct so it's a value type. Typically value types are allocated on the stack instead of the heap. However, you're array is most likely (please correct me if I'm wrong here) is a reference type and will be allocated on the heap. So I'm not sure exactly where the value types memory will be allocated. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

              J Offline
              J Offline
              Julian Bucknall MSFT
              wrote on last edited by
              #6

              Arrays are reference types, therefore they're always allocated on the heap. An array of structs is no different -- it'll be on the heap -- however, in this case the memory block on the heap will be a contiguous series of Data structs, one after the other. All that the "new Data()" calls are doing is initializing the various elements of the array. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

              J 1 Reply Last reply
              0
              • J Julian Bucknall MSFT

                Arrays are reference types, therefore they're always allocated on the heap. An array of structs is no different -- it'll be on the heap -- however, in this case the memory block on the heap will be a contiguous series of Data structs, one after the other. All that the "new Data()" calls are doing is initializing the various elements of the array. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #7

                That's music to my ears.:) Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups