# C Sharp/Objects

### System.Object

• `GetHashCode()` - 獲取對象的獨一無二Id
• `GetType()` -
• `ToString()` -

## 對象

### 構造函數

#### 拷貝構造函數

```using System;

namespace CopyConstructor
{
class Rectangle
{
public int length;

public Rectangle(int x, int y)         // constructor fn
{
length = x;
}

public Rectangle(Rectangle r)
{
length = r.length;
}

public void display()
{
Console.WriteLine("Length = " + length);
}
}   // end of class Rectangle

class Program
{
public static void Main()
{
Rectangle r1 = new Rectangle(5, 10);
Console.WriteLine("Values of first object");
r1.display();

Rectangle r2 = new Rectangle(r1);
Console.WriteLine("Values of second object");
r2.display();

}
}
}
```

#### 靜態構造函數

```using System;
using System.Collections.Generic;
using System.Text;

namespace StaticConstructors
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int j = 0;
Console.WriteLine("Static Number = " + MyClass.Number);
}
}

class MyClass
{
private static int _number;
public static int Number { get { return _number; } }
static MyClass()
{
Random r = new Random();
_number = r.Next();
}
}
}
```

#### 缺省構造函數

```// Created by the developer
class MyClass
{
}

// Created by the compiler
class MyClass : System.Object
{
public MyClass() : base()
{
}
}
```

#### 重載構造函數

``` class MyClass
{
private int _number;
public int Number { get { return _number; } }

public MyClass()
{
Random randomNumber = new Random();
_number = randomNumber.Next();
}

public MyClass(int seed)
{
Random randomNumber = new Random(seed);
_number = randomNumber.Next();
}
}
```

#### 調用其他構造函數

``` class MyClass
{
private int _number;
public int Number { get { return _number; } }

public MyClass() :
this ( DateTime.Now.Milliseconds ) // Call the other constructor passing in a value.
{

}

public MyClass(int seed)
{
Random r = new Random(seed);
_number = r.Next();
}
}
```

``` class MyException : Exception
{
private int _number;
public int Number { get { return _number; } }

public MyException ( int errorNumber, string message, Exception innerException)
: base( message, innerException )
{
_number = errorNumber;
}
}
```

### 析構函數和Dispose方法

• 託管資源：由CLR創建和釋放
• 非託管資源：資源的創建和釋放不由CLR管理。比如IO、網絡連接、數據庫連接等等。需要開發人員手動釋放。

```class Test
{
// Some Code
~Test
{
//Necessary cleanup code
}
}
//将被自动编译为：
protected override void Finalize()
{
try
{
//Necessary cleanup code
}
finally
{
base.Finalize();
}
}
```

```  public class Base: IDisposable
{
private bool isDisposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); //要求runtime不要再调用这个终结器了。因为重复释放非受管资源会造成不可控后果。
}
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if (disposing)
{
// Code to dispose managed resources
// held by the class
}
}
// Code to dispose unmanaged resources
// held by the class
isDisposed = true;
base.Dispose(disposing);
}

~Base()
{
Dispose (false);
}
}

public class Derived: Base
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Code to cleanup managed resources held by the class.
}
// Code to cleanup unmanaged resources held by the class.
base.Dispose(disposing);
}
// Note that the derived class does not // re-implement IDisposable
}
```