Array question
-
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
-
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
What bothers you about this? Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n
-
What bothers you about this? Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n
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
-
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
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. -
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
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
-
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
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.
-
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.
That's music to my ears.:) Joe Woodbury When all else fails, there's always delusion. - Conan O'Brien