There is absolutely nothing wrong with your code. Doing what you did:
Code: Select all
vertices = new VertexPositionColor[3];
vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red);
vertices[1] = new VertexPositionColor(new Vector3(0.5f, 0, 0), Color.Green);
vertices[2] = new VertexPositionColor(new Vector3(-0.5f, 0, 0), Color.Blue);
Is equivalent to:
Code: Select all
vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0.5f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
vertices[2].Color = Color.Yellow;
The only difference is the fact that you are allowing your constructor to initialize your fields up on the VertexPositionColor's construction. The constructor method is not going to be any slower.
EccentricDuck wrote:I listed why I program the way I do - extensive use of constructors for efficiency of time, use of private members, and (perhaps less mentioned) to keep the necessary members bundled together in an easy to reference location.
Good for you. There's absolutely nothing wrong with that. It's considered good OO design.
EccentricDuck wrote:[..]and wouldn't accessing each individual field require making all those fields public? By using a constructor call I can keep most fields private. Just something that came to mind.
Oh, most definitely. That is considered good design. In this particular case (VertexPositionColor) there is no real reason for the XNA framework to hide (or "encapsulate") the Color or Position values. I'm pretty sure they are actually implemented as "properties" (according to the official C# style guide, the capitalized field names are denoting properties).
Traditionally (when I say traditionally, I refer to C++), member variables (is what we call 'em) are private when their change has an effect on something else. Imagine a class where changing the value of a variable has an effect on something else. When I change something like the shape of a RigidBody class, that change means that I have to recalculate the center of mass, the mass, moment of inertia, and a few other things. Now instead of making the programmer do it manually (be responsible for updating other variables when one changes) like in the days of C, this functionality is "encapsulated" or hidden from the user.
In C#, we are seeing a little bit of a change. With properties we don't really see get/set member functions ("methods" in C#) any more--instead we see "properties" which allow us to encapsulate this logic and allow our end-user to access our fields stupidly as if they were mere variables.
In the case of this particular scenario (VertexPositionColor), we have a very trivial little class where there is no real reason to hide any of the variables from the user. As a matter of fact, the "class" is probably actually a "struct" for this reason--a value type, rather than a reference type. This means that it's stored on the stack rather than the heap. It is recommended that you use that approach for trivial classes that require no real encapsulation and exposes all fields. Since it is stored on the stack, its allocation is also faster than reference types. It will also be deleted or "popped off the stack" the minute the function (errr... I mean "method") finishes execution without the CLR having to run the garbage collector on it.