Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualNypyren

Posted 14 November 2012 - 10:31 PM

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.


I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct, except in the very rare case where your struct instance is a local variable.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them. I've *never* needed to use structs to address performance issues, and misuse of structs can actually hurt performance compared to classes.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches.

#4Nypyren

Posted 14 November 2012 - 10:24 PM

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.


I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct, except in the very rare case where your struct instance is a local variable.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches.

#3Nypyren

Posted 14 November 2012 - 10:22 PM

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.


I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches.

#2Nypyren

Posted 14 November 2012 - 10:21 PM

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.


I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches.

#1Nypyren

Posted 14 November 2012 - 10:15 PM

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.


I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them.

PARTNERS