struct-vs-class

Enumerator Implementation: Use struct or class?

会有一股神秘感。 提交于 2020-01-01 08:28:28
问题 I noticed that List<T> defines its enumerator as a struct , while ArrayList defines its enumerator as a class . What's the difference? If I am to write an enumerator for my class, which one would be preferable? EDIT: My requirements cannot be fulfilled using yield , so I'm implementing an enumerator of my own. That said, I wonder whether it would be better to follow the lines of List<T> and implement it as a struct. 回答1: Like this others, I would choose a class. Mutable structs are nasty.

When are structs the answer?

血红的双手。 提交于 2019-12-29 02:44:31
问题 I'm doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you create millions of them, they don't live longer than a single method, they're lightweight. However, by simply changing 'struct' to 'class' on Vector and Ray, I got a very significant performance gain. What gives? They're both small (3 floats for Vector, 2 Vectors for a Ray), don't get copied around excessively. I do pass

Enumerator Implementation: Use struct or class?

核能气质少年 提交于 2019-12-04 01:47:52
I noticed that List<T> defines its enumerator as a struct , while ArrayList defines its enumerator as a class . What's the difference? If I am to write an enumerator for my class, which one would be preferable? EDIT: My requirements cannot be fulfilled using yield , so I'm implementing an enumerator of my own. That said, I wonder whether it would be better to follow the lines of List<T> and implement it as a struct. Like this others, I would choose a class. Mutable structs are nasty. (And as Jared suggests, I'd use an iterator block. Hand-coding an enumerator is fiddly to get right.) See this

When are structs the answer?

谁都会走 提交于 2019-11-28 16:32:15
I'm doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you create millions of them, they don't live longer than a single method, they're lightweight. However, by simply changing 'struct' to 'class' on Vector and Ray, I got a very significant performance gain. What gives? They're both small (3 floats for Vector, 2 Vectors for a Ray), don't get copied around excessively. I do pass them to methods when needed of course, but that's inevitable. So what are the common pitfalls that kill