Thursday, March 31, 2011

learn c sharp Gaming Chapter 6


Values versus References
There are different ways for a compiler to talk about data, and C# has two different ways
to do so. All datatypes in C# fall into one of two categories:
_ Value types
_ Reference types
I’ll explain each of these different kinds of types in the following sections.
Value Types
A value type is typically a small piece of data that the system spends very little time managing.
You have already used value types in Chapter 2 with all of the built-in numeric data
types. —such as ints, floats, and so on—is a value type.

note
Value types are created on the system stack. You don’t necessarily need to know what that is, but
if you’re interested, I strongly urge you to research it on your own. This topic goes beyond the scope
of this book, so I don’t have enough room to explain it here, but it greatly helps you understand
exactly how computers work, which will in turn make your programs faster and more efficient.

Value types are fairly simple and straightforward to use, as you can see in this code:
int x = 10, y = 20;
x = y; // value of y is copied into x
y = 10; // y is set to 10
Along with the built-in data types, structures are value types as well, which I explore in
much more detail later in this chapter.

Reference Types
Reference types are completely different from value types. Classes, unlike structures, are
always reference types. Reference types, rather than storing the data directly, store an
address inside of them, and that address points to the actual data in the computer somewhere.

Declaring a Reference Type
One of the biggest differences between values and references lies in the way you declare
them. A reference type must be created using the new keyword (pretend we have a class
named Foo):
Foo x = new Foo();
36 Chapter 3 _ A Brief Introduction to Classes
That may look like a lot of work at first, but you’ll get used to it. Basically, the code is performing
two tasks. It is:
1. creating a new reference type named x, and
2. creating a new Foo object on the heap and making x point to it.

note
The heap is another part of the computer that stores memory. I don’t have enough room to explain
it here; this is something else you should research on your own if you’re interested.
Of course, you don’t have to do that all at once. You could easily split it up like this:
Foo x;
x = new Foo();
It’s up to you.

Playing with References
Now it’s time to play around with references, which is something you haven’t done before.
Unfortunately for you, references don’t exactly work the same way as value types, and this
can be kind of confusing at first.
Value types are stored directly, whereas reference
types store an address pointing to the actual data.

This is where references tend to get a bit tricky. You absolutely must remember at all times
that you are using references, or else you will end up with programs that don’t act the way
you want them to act. For example, try to guess what this code does:
Foo x = new Foo();
Foo y = new Foo();
y = x;
// perform some operation that changes y here
You’d think that after this code executes, x would be in its original state and y would be
changed, right? Wrong! They’re both changed. Bear with me—it’s a little difficult to see at
first, but it makes sense. A diagram may help;
Basically, the line that really messes everything up is the following:
y = x;
What does that actually accomplish? You probably wanted to copy the data from x into y,
but that didn’t happen. Instead, as they are both reference types, the computer makes y
point to the same data that x is pointing to. So x and y are now pointing to the same data
in memory, and performing any operation on y will do the same to x.
note
If you really wanted to copy a reference type into a new reference type rather than just making two
references point to the same data, you need to use a built-in C# function to perform a clone of the
class. You can see this done with the GameObject class in Chapter 10.
38 Chapter 3 _ A Brief Introduction to Classes
Figure 3.2 Assigning x to y makes y point to x’s data, and doesn’t actually
copy the value as you would expect.

Garbage Collection
In the example shown in Figure 3.2, you may have noticed that y was given some memory,
which it then ignored once it was assigned to x.What happens to that memory that y was
pointing to?
In older languages, like C, the memory would be lost forever. You would be creating what
is called a dangling pointer (pointers are like references); the computer knows that the
memory is being used, but your program forgot where it was, and you’ll never be able to
reclaim that memory until you shut down the program.
C# solves this problem using garbage collection. Every time you create a new piece of data
in C#, the .NET runtime keeps track of how many times your program is pointing at that
data, and if that number ever goes down to 0, then the garbage collector will detect it and
release that memory for something else to use.
It is impossible to create memory leaks in C#.
null
There’s a special value that you can use with reference types; it’s called null. The null value
essentially means “nothing.” If you set a reference to null, then you’re telling the computer
that the reference is pointing to nothing at all. Older languages used the value 0 to denote
this, but null is more readable.

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...