財產

2010-04-28 133 views
-1

通用接口我有一個接口財產

/// <summary> 
/// Summary description for IBindable 
/// </summary> 
public interface IBindable<T> 
{ 
    // Property declaration: 
    T Text 
    { 
     get; 
     set; 
    } 
} 

現在我想實現這個接口在我的課

public class MyTextBox :IBindable<string> 
{ 
    //now i how can i implement Text peroperty here 
} 

我不想要實現它像

string IBindable<string>.Text 
{ 
    get { return "abc";} 
    set { //assigne value } 
} 

我想實現它像

public string Text 
{ 
    get{} set {} 
} 
+0

我不能把握的問題......你可以做你說什麼。 .. – digEmAll 2010-04-28 17:53:32

+0

這裏有什麼問題? – 2010-04-28 17:55:01

+0

我猜OP沒有意識到這是有效的...... – 2010-04-28 17:56:08

回答

5

您可以自由做到這一點。這是一個隱含的接口實現。

以下是有效的C#:

public interface IBindable<T> 
{ 
    // Property declaration: 
    T Text 
    { 
     get; 
     set; 
    } 
} 

public class MyTextBox : IBindable<string> 
{ 

    public string Text 
    { 
     get; 
     set; 
    } 
} 

當你實現一個接口,你可以自由地隱式實現它,因爲上面,或者明確地說,這將是你的第二個選項:

string IBindable<string>.Text 
{ get { return "abc";} set { // assign value } } 

區別在於使用。當您使用第一個選項時,Text屬性成爲該類型本身的公開可見屬性(MyTextBox)。這允許:

MyTextBox box = new MyTextBox(); 
box.Text = "foo"; 

但是,如果你明確地實現它,你需要直接使用你的接口:

MyTextBox box = new MyTextBox(); 
IBindable<string> bindable = box; 
box.Text = "foo"; // This will work in both cases 
2
public class MyTextBox : IBindable<string> 
{ 
    //now i how can i implement Text peroperty here 
    public string Text 
    { 
     get; 
     set; 
    } 
}