Defining classes 73 example 3 13 storing the

Info icon This preview shows pages 97–100. Sign up to view the full content.

View Full Document Right Arrow Icon
Defining Classes | 73
Image of page 97

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

View Full Document Right Arrow Icon
Example 3-13. Storing the conversion factor in a field class Plane { // Constructor with a parameter public Plane(string newIdentifier) { Identifier = newIdentifier; } public string Identifier { get; private set; } double kilometersPerMile = 1.609344; public double SpeedInMilesPerHour { get { return SpeedInKilometersPerHour / kilometersPerMile ; } set { SpeedInKilometersPerHour = value * kilometersPerMile ; } } public double SpeedInKilometersPerHour { get; set; } } Notice how we’re able to initialize the field to a default value right where we declare it, by using the = operator. (This sort of code is called, predictably enough, a field initial- izer .) Alternatively, we could have initialized it inside a constructor, but if the default is a constant value, it is conventional to set it at the point of declaration. What about the first example of a field that we saw—the one we used as the backing data for a property in Example 3-12 ? We didn’t explicitly initialize it. In some other languages that would be a ghastly mistake. (Failure to initialize fields correctly is a major source of bugs in C++, for example.) Fortunately, the designers of .NET decided that the trade-off between performance and robustness wasn’t worth the pain, and kindly initialize all fields to a default value for us—numeric fields are set to zero and fields of other types get whatever the nearest equivalent of zero is. (Boolean fields are initialized to false , for example.) 74 | Chapter 3: Abstracting Ideas with Classes and Structs
Image of page 98
There’s also a security reason for this initialization. Because a new ob- ject’s memory is always zeroed out before we get to see it, we can’t just allocate a whole load of objects and then peer at the “uninitialized” values to see if anything interesting was left behind by the last object that used the same memory. Defining a field for our scale factor is an improvement, but we could do better. Our 1.609344 isn’t ever going to change. There are always that many kilometers per mile, not just for this instance of a Plane , but for any Plane there ever will be. Why allocate the storage for the field in every single instance? Wouldn’t it be better if we could define this value just once, and not store it in every Plane instance? Fields Can Be Fickle, but const Is Forever C# provides a mechanism for declaring that a field holds a constant value, and will never, ever change. You use the const modifier, as Example 3-14 shows. Example 3-14. Defining a constant value const double kilometersPerMile = 1.609344; The platform now takes advantage of the fact that this can never change, and allocates storage for it only once, no matter how many instances of Plane you new up. Handy. This isn’t just a storage optimization, though. By making the field const , there’s no danger that someone might accidentally change it for some reason inside another func- tion he’s building in the class—the C# compiler prevents you from assigning a value to a const field anywhere other than at the point of declaration.
Image of page 99

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

View Full Document Right Arrow Icon
Image of page 100
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