Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

.net - struct vs class

This applies .NET. I am looking to write a spatial mapping application. There will be several polygons in memory at once (on the order of 30 - 50 polygon). each polgon has a collection of LatLong points. the collection can range from 10 to 200 per polygon. However, there are alot of calculations that will be done using the points, which is why (for performance) I want to make the LatLong a struct. However I am weary do to the large number of LatLong that will be in memory. Any insight to this will be appreciated. To recap: I want to know if I shold make the LatLong a struct because I want performance ont the calculation, or a class because of the number of latLongs that will be in memory at one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer to this is going to depend on your resource priorities, what's in the class/struct, and how you're using it. I suggest you figure out what your memory/performance resources are like, then do a lot of testing of both implementations to see how they fit in those resource parameters. If possible, try to test the actual operations you expect to perform.

MSDN also offers some good guidance on when and when not to use structs. From the article:

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...