2011-08-26 53 views
3

播放各地在單2.10 REPL:String a; a.ToBoolean(...)IConvertible

csharp> string a = "true"; 
csharp> a.ToBoolean(CultureInfo.InvariantCulture); 
{interactive}(1,4): error CS1061: Type `string' does not contain a definition fo 
r `ToBoolean' and no extension method `ToBoolean' of type `string' could be foun 
d (are you missing a using directive or an assembly reference?) 
C:\PROGRA~1\MONO-2~1.2\lib\mono\4.0\mscorlib.dll (Location of the symbol related 
to previous error) 


csharp> ((IConvertible)a).ToBoolean(CultureInfo.InvariantCulture); 
true 
csharp> 

根據文檔,System.String實現IConvertible。如果這是真的,爲什麼

a.ToBoolean(CultureInfo.InvariantCulture); 

失敗?

爲什麼我必須將它轉換爲IConvertible才能使ToBoolean正常工作?

+0

在任何情況下,您都不應該使用這些轉換方法,而應該爲該類型或Convert類定義的解析函數。 –

回答

2

該接口被明確實現。

+0

可以直接調用隱式實現的接口。 – erikH

+2

接口沒有明確實現,它的成員(如方法)是。例如,您可以隱式地和另一個顯式地實現一個成員。 – svick

+0

@svick沒錯。另見http://stackoverflow.com/questions/143405/c-interfaces-implicit-and-explicit-implementation – erikH

相關問題