Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
That's the System.Object class folks!
-
0:00
To wrap up this part of the course, let's go back and
-
0:03
take a look at the list of methods the system.object provides.
-
0:07
As we've seen we can override most of these methods
-
0:09
with their own implementations.
-
0:11
We've seen how overwriting ToString can provide a better conversion
-
0:15
of objects to string.
-
0:17
The default implementation provided by system.object is
-
0:21
just to print the object's type name.
-
0:23
We've seen how we can redefine what it means for
-
0:26
two objects to be equal by overriding equals and get hash code.
-
0:31
There are a couple more methods in system.object, we haven't looked at yet.
-
0:35
Finalize is used by the memory management system,
-
0:39
also known as the garbage collector.
-
0:41
You see every time a new object is created,
-
0:44
it's stored some place in the computer's memory.
-
0:47
We may create lots and
-
0:48
lots of objects, eventually we don't need access to some of these objects any more.
-
0:54
We don't have any way to delete them from memory and free up that space for
-
0:58
other objects, so these objects can just sit in memory taking up space.
-
1:04
Periodically, .NET memory management system runs the garbage collector which
-
1:09
goes through the memory and
-
1:10
finds objects that aren't being used by the program anymore.
-
1:13
It removes them from the memory and frees up the space.
-
1:17
We're given the opportunity to run some final code
-
1:20
right before the object is removed, that's the purpose of the finalize method.
-
1:26
We can override the finalize method and write whatever code we want there.
-
1:30
Typically, finalize is used to close files and
-
1:33
clean up any other computer resources that the object may have been using.
-
1:37
Most of the time we don't need to think about the finalizer method.
-
1:41
Writing finalizer methods is something that is rare,
-
1:44
so we won't go into it right now.
-
1:46
The MemberwiseClone method is exactly what it sounds like.
-
1:50
It is used to create a copy of an object.
-
1:53
The method is protected so it's only accessible from within the subclass.
-
1:57
But it also can't be overridden by subclasses because it's not virtual,
-
2:02
this method is rarely used in practice.
-
2:05
So that's the system.object class.
-
2:08
In most cases, we don't interact with this class directly,
-
2:12
instead we use it indirectly through a subclass.
-
2:16
Because all classes inherit from system.object,
-
2:19
we can count on all objects having these methods.
-
2:23
This is just one more example of how polymorphism
-
2:27
with virtual methods is useful.
-
2:29
In the next part of this course, we'll learn about the final basic principle of
-
2:33
object oriented programming, abstraction.
-
2:36
We'll learn how C # supports this principle by
-
2:39
further extending the tree house defense game.
You need to sign up for Treehouse in order to download course files.
Sign up