By DimitriC at August 15, 2010 10:40
Filed Under:
Every object in .NET is derived from System.Object. Even when you create your own object, you’ll see that automatically there are four methods that you can use: Equals(), GetHashCode(), ToString() and GetType(). There are also two protected methods available: MemberwiseClone() and Finalize(). In this post I will talk about the two protected members and the public methods will be discussed in part II and part III.
Example:
1: public class Car
2: {
3: private string color;
4: private int nrOfSeats;
5: private string brand;
6: private string model;
7:
8: public Car(string color, int nrOfSeats, string brand, string model)
9: {
10: this.color = color;
11: this.nrOfSeats = nrOfSeats;
12: this.brand = brand;
13: this.model = model;
14: }
15:
16: public string Model
17: {
18: get { return model; }
19: set { model = value; }
20: }
21:
22: public string Brand
23: {
24: get { return brand; }
25: set { brand = value; }
26: }
27:
28:
29: public int NumberOfSeats
30: {
31: get { return nrOfSeats; }
32: set { nrOfSeats = value; }
33: }
34:
35:
36: public string Color
37: {
38: get { return color; }
39: set { color = value; }
40: }
41:
42: }
MemberwiseClone
This protected method gives you the possibility to clone your object. This copy of your object is called a shallow copy. A new object will be created and all the non-static fields of the object on which you called this method, will be copied to the new object. Now, if you have references to other objects, MemberwiseClone will copy these references as well. This pitfall is something that is often overlooked. References in your new object will still point to existing objects. If you wish to copy these too, implement the IClonable interface. This interface only has one member, which is Clone(). Now you can override this method and create new instances of these objects, linking them to the new object. As a result you will have a new object, referencing new objects.
In my Car class, I created a CopyCar() method which calls the MemberwiseClone. Since the Car object has no references to other objects, the copy of the object that is returned is independent from the existing object. Note that MemberwiseClone returns an object of type Object. You will need to cast is explicitly to your own object.
1: public Car CopyCar()
2: {
3: Car copy = (Car)this.MemberwiseClone();
4:
5: return copy;
6: }
Finalize
When the garbage collector sees this object isn’t used anymore, this is the method that it calls to reclaim the memory used by the object. So if your object requires any special clean-up actions (for example: freeing external resources or freeing unmanaged resources), you should override this method so you can implement the necessary clean-up actions. When implementing Finalize in a derived type, you must not forget to call the base Finalize method. This actually is more of a subject to include in a post about the garbage collector (someday!).
84a78137-b3be-47a3-8a04-6ec8c2a25614|0|.0
Tags: