Posts
[C#]透過API移除表單的控制功能表方塊中的功能表項目
const Int32 MF_BYPOSITION = 1024;//&H400 public enum SystemMenuItemType { Restore, Move, Size, Min, Max, Seperator, Close } public static void RemoveSystemMenuItem(Form form, SystemMenuItemType menuItem) { RemoveMenu(GetSystemMenu(form.Handle.ToInt32(), 0),(int)menuItem, MF_BYPOSITION); } public static void RemoveRestoreSystemMenuItem(Form form) { RemoveSystemMenuItem(form, SystemMenuItemType.Restore); } public static void RemoveMoveSystemMenuItem(Form form) { RemoveSystemMenuItem(form, SystemMenuItemType.Move); } public static void RemoveSizeSystemMenuItem(Form form) { RemoveSystemMenuItem(form, SystemMenuItemType.Size); } public static void RemoveMinSystemMenuItem(Form form) { RemoveSystemMenuItem(form, SystemMenuItemType.Min); } public static void RemoveMaxSystemMenuItem(Form form) { RemoveSystemMenuItem(form, SystemMenuItemType.
read morePosts
[C#]Effective C# 條款十一: 優先採用foreach迴圈
foreach (int i in foo) Console.WriteLine(i.ToString()); for (int index = 0; index < len; index++) Console.WriteLine(foo[index].ToString()); for (int index = 0; index < foo.Length; index++) Console.WriteLine(foo[index].ToString()); }</pre>
read morePosts
[C#]Effective C# 條款九: 理解幾個相等判斷之間的關係
//兩個有一個為空=>return false; if ((left == null) || (right == null)) return false; //透過left.Equals判斷是否相等 return left.Equals(right); }
//檢查是否與自身是相同物件 if (object.ReferenceEquals(this, obj)) return true; //檢查是否型態相等 if (this.GetType() != obj.GetType()) return false; //比較內容是否相等 return CompareMembers(this, obj); }</pre> //檢查是否與自身是相同物件 if (object.ReferenceEquals(this, obj)) return true; //檢查是否型態相等 if (this.GetType() != obj.GetType()) return false; //叫用基底類別的Equals方法 if(!base.Equals(obj)) return false; //比較內容是否相等 return CompareMembers(this, obj); }</pre>
read morePosts
[C#]Effective C# 條款八: 確保0為值類型的有效狀態
struct Person { public String Name; public Sex Sex; public Person(String name, Sex sex) { this.Name = name; this.Sex = sex; } }</pre> struct Person { public String Name; public Sex Sex; public Person(String name, Sex sex) { this.Name = name; this.Sex = sex; } }</pre>
read morePosts
[C#]Effective C# 條款七: 將值類型盡可能實現為具有常量性與原子性的類型
public string Line { get { return _line; } set { _line = value; } } public string City { get { return _city; } set { _city = value; } } public string State { get { return _state; } set { ValidateState(value); _state = value; } } public int ZipCode { get { return _zipCode; } set { ValidateZip(value); _zipCode = value; } } ... }</pre> public string Line { get { return _line; } } public string City { get { return _city; } } public string State { get { return _state; } } public int ZipCode { get { return _zipCode; } } public Address(string line,string city,string state,int zipCode) { _line = line; _city = city; _state = state; _zipCode = zipCode; ValidateState(state); ValidateZip(zipCode); } }</pre> public PhoneList(Phone[] ph) { _phones = ph; } .
read morePosts
[C#]Effective C# 條款六: 明辨值類型與參考類型的使用場合
MyData data= GetData();
MyData data= GetData();
IMyInterface data= GetData();
C var = new C();
public void Pay(BankAccount b) { b.Balance -= _salary; } }
public void Pay(BankAccount b) { b.Balance -= _salary; } }
read morePosts
[C#]Effective C# 條款四: 使用ConditionalAttribute替代#if條件編譯
[Conditional(“B”)] static void DoIfAandB() { // Code to execute when both A and B are defined… } static void HelloWord() { Console.WriteLine("Hello~"); } static void SayHello1() { #if DEBUG HelloWord(); #endif }
[System.Diagnostics .Conditional ("DEBUG")] static void SayHello2() { HelloWord(); }</pre> private static void Main(string[] args) { HelloWord(); SayHello1(); //SayHello2被編譯器拿掉 } private static void SayHello1() { //內容被編譯器拿掉 } [Conditional("DEBUG")] private static void SayHello2() { HelloWord(); } }
read morePosts
[C#]使用DebuggerDisplayAttribute自訂除錯監看訊息
[System.Diagnostics.DebuggerDisplay("Name = {Name}, Year = {Year}")] class Person { [System.Diagnostics.DebuggerDisplay("Name = {Name}")] string Name { get; set; } int Year { get; set; } public Person(string name, int year) { this.Name = name; this.Year = year; } }</pre></div>
read more