在slice中我们有如下定义:
Dictionary<long, Employee> EmployeeMap;
Slice2CS编译器会将上面的字典生成同名的C#类:
public class EmployeeMap : System.Collections.DictionaryBase,
System.ICloneable
{
public Employee this[long key] { get; set; }
public System.Collections.ICollection Keys { get; }
public System.Collections.ICollection Values { get; }
public void AddRange(EmployeeMap d);
public void Add(long key, Employee value);
public void Remove(long key);
public bool Contains(long key);
public object Clone();
public override int GetHashCode();
public override bool Equals(object other);
public static bool Equals(EmployeeMap lhs,
EmployeeMap rhs);
public static bool operator==(EmployeeMap lhs,
EmployeeMap rhs);
public static bool operator!=(EmployeeMap lhs,
EmployeeMap rhs);
}
注意生成的类继承自DictionaryBase基类,所以也就拥有基类的方法。利用继承自基类的AddRange方法我们可以将一个字典的内容附加到另一个字典。如果目标字典和源字典的键有冲突的话,目标字典的键不会被覆盖。例如:
Employee e1 = new Employee();
e1.number = 42;
e1.firstName = "Herb";
e1.lastName = "Sutter";
EmployeeMap em1 = new EmployeeMap();
em[42] = e1;
Employee e2 = new Employee();
e2.number = 42;
e2.firstName = "Stan";
e2.lastName = "Lipmann";
EmployeeMap em2 = new EmployeeMap();
em[42] = e2;
// Add contents of em2 to em1
em1.AddRange(em2);
// Equal keys preserve the original value
Debug.Assert(em1[42].firstName.Equals("Herb"));
生成的代码也提供了GetHashCode、Equals、== 、!=方法(两个字典只有在它们包含的条目一样多,并且每个条目的键和值都相同时,才被认为是相等的)。
生成的Clone方法依然是浅copy。