The enum we declared earlier and the built in numeric

Info icon This preview shows pages 107–109. Sign up to view the full content.

View Full Document Right Arrow Icon
The enum we declared earlier and the built-in numeric types ( int , double ) behave dif- ferently, though, as Example 3-27 illustrates. Example 3-27. Copying values, not references int firstInt = 3; int secondInt = firstInt; secondInt = 4; if (firstInt != 4) { Console.WriteLine("Well. They're not the same at all."); } When we assign firstInt to secondInt , we are copying the value . In this case, the var- iables hold the actual value, not a reference to a value. We call types that behave this way value types . Value Types and Reference Types | 83
Image of page 107

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

View Full Document Right Arrow Icon
People often refer to reference types as being allocated “on the heap” and value types “on the stack.” C++ programmers will be familiar with these concepts, and C++ pro- vided one syntax in the language to explicitly create items on the stack (a cheap form of storage local to a particular scope), and a different syntax for working on the heap (a slightly more expensive but sophisticated form of storage that could persist beyond the current scope). C# doesn’t make that distinction in its syntax, because the .NET Framework itself makes no such distinction. These aspects of memory management are completely opaque to the developer, and it is actively wrong to think of value types as being always allocated on a stack. For people familiar with C++ this can take a while to get used to, especially as the myth is perpetuated on the Web, in the MSDN documentation and elsewhere. (For example, at the time of this writing, states that structs are created on the stack, and while that happens to be true of the ones in that example when running against the current version of .NET, it would have been helpful if the page had mentioned that it’s not always true. For example, if a class has a field of value type, that field doesn’t live on the stack—it lives inside the object, and in all the versions of .NET released so far, objects live on the heap.) The important difference for the C# developer between these two kinds of types is the one of reference versus copy semantics. As well as understanding the difference in behavior, you also need to be aware of some constraints. To be useful, a value type should be: Immutable Lightweight Something is immutable if it doesn’t change over time. So, the integer 3 is immutable. It doesn’t have any internal workings that can change its “three-ness”. You can replace the value of an int variable that currently contains a 3 , by copying a 4 into it, but you can’t change a 3 itself. (Unlike, say, a particular Plane object, which has a Direction property that you can change anytime you like without needing to replace the whole Plane .) There’s nothing in C# that stops you from creating a mutable value type. It is just a bad idea (in general). If your type is mutable, it is prob- ably safer to make it a reference type, by declaring it as a class. Mutable value types cause problems because of the copy semantics—if you mod- ify a value, it’s all too easy to end up modifying the wrong one, because there may be many copies.
Image of page 108
Image of page 109
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