Anytime we write a struct c automatically generates a

Info icon This preview shows pages 110–112. Sign up to view the full content.

View Full Document Right Arrow Icon
Anytime we write a struct , C# automatically generates a default, parameterless con- structor that initializes all of our storage to zero, so if we don’t want to write any custom constructors, we won’t have any problems. (Unlike with a class , we aren’t allowed to replace the default constructor. We can define extra constructors, but the default con- structor is always present and we’re not allowed to write our own—see the sidebar on the next page for details.) Example 3-28 has hit trouble because we’re trying to provide an additional constructor, which initializes the properties to particular values. If we write a constructor in a struct , the compiler refuses to let us invoke any methods until we’ve initialized all the fields. (It doesn’t do the normal zero initialization for custom constructors.) This re- striction turns out to include properties, because get and set accessors are methods under the covers. So C# won’t let us use our properties until the underlying fields have been initialized, and we can’t do that because these are auto properties—the C# com- piler has generated hidden fields that we can only access through the properties. This is a bit of a chicken-and-egg bootstrapping problem! Fortunately, C# gives us a way of calling one of our constructors from another. We can use this to call the default constructor to do the initialization; then our constructor can set the properties to whatever values it wishes. We call the constructor using the this keyword, and the standard function calling syntax with any arguments enclosed in parentheses. As Example 3-29 shows, we can invoke the default constructor with an empty argument list. 86 | Chapter 3: Abstracting Ideas with Classes and Structs
Image of page 110

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Value Types and Default Constructors Why aren’t we allowed to define a custom default constructor for a value type, given that we’re allowed to do that for a reference type? The short answer is that the speci- fication for the relevant behavior in the .NET Framework doesn’t let you. (The speci- fication in question is called the Common Language Infrastructure [CLI], incidentally.) The slightly longer answer is: for efficiency reasons. By mandating that the default constructor for any value type always initializes everything to zero, large arrays of value types can be constructed very cheaply, just by allocating the required amount of mem- ory and zeroing out the whole array in one step. And similarly, it simplifies the initial- ization of fields and variables—everything can be initialized to zero. Example 3-29. Calling one constructor from another public PolarPoint3D(double distance, double angle, double altitude) : this() { Distance = distance; Angle = angle; Altitude = altitude; } You add the call just before the opening brace for the body of the constructor, and prefix it with a colon. We can also use this technique to avoid writing common initi- alization code multiple times. Say we wanted to provide another utility constructor that just took the polar coordinates, and initialized the altitude to zero by default. Instead
Image of page 111
Image of page 112
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern