Attributes是用來註釋(annotation) assemblies, typs, members and parameters
使用中刮號[]加在要被註釋的元件上面,如下
[AttributeUsage(...)]
public class NameAttribute : Attribute{
...
}
並且可以不只一個Attributes
public class NameAttribute : Attribute
[AttributeUsage(...)]
public class NameAttribute
public class NameAttribute : Attribute{
public int Age {get{...} set{...}}
}
public class NameAttribute : Attribute{
public NameAttribute(string userName) {...}
public string UserName { get{....} }
}
// Don't do this
public class NameAttribute : Attribute{
public NameAttribute(int age) {...}
public int Age {get{...} set{...}}
}
避免overload客制的建構子
如果要加快搜尋速度,苦以使用sealed
public sealed class NameAttribute : Attribute {...}
Uri 全名為 uniform resource identifier,用字串來識別資源
使用System.Uri表示URI 及 URL
傳入Uri型態的方法,可以考慮多加一個傳入string的overload
public void NavigeteTo(Uri location){...}
pubilc void NavageteTo(string location){
NavigeteTo(new Uri(location);
}
但是不要每個傳入Uri型態的方法,都多一個string的overload
根據需要的情境選擇性overload即可
實做System.Uri時,call the Uri-based overloads
不要把URI/URL資料存成string
即使傳入string,也要轉換成Uri使用
public class SomeResource{
Uri location;
public SomeResource(string location){
this.location = new Uri(location);
}
public SomeResource(Uri location){
this.location = new location;
}
}