效能議題-Contains與indexof的比較

摘要:效能議題-Contains與indexof的比較

一般都會認為contains比indexof效能來的好,來的快

根據http://stackoverflow.com/questions/498686/net-is-string-contains-faster-than-string-indexof

這篇文章寫到

By using Reflector, you can see, that Contains is implemented using IndexOf. Here's the implementation.

public bool Contains(string value) 
{ 
   return (this.IndexOf(value, StringComparison.Ordinal) >= 0); 
} 

So Contains is likely a wee bit slower than calling IndexOf directly, but I doubt that it will have any significance for the actual performance.

所以其實Contains也是呼叫indexof這個方法來比較字串,

關於StringComparison 列舉型別:

http://msdn.microsoft.com/zh-tw/library/system.stringcomparison(VS.95).aspx

 

關於indexof參數:

http://msdn.microsoft.com/zh-tw/library/ms224425(v=VS.80).aspx