2014-10-09 29 views
0

我發現這樣ReadAsAsync<T>(this HttpContent content);在C#中的一些方法,我不知道什麼樣的方法是和我的頭上創建新方法的C#方法名<Variable>(可變)

質疑彈出「這是可能的創建像這樣的方法 'methodName<varible>(variable){}'並且此方法是否存在?「

例如:

public void methodName<string getText>(string text) 
{ 
    getText = text; 
} 

當我調用該方法:

string sampleText; 
methodName<sampleText>("Hello World"); 

所以sampleText的價值將成爲"Hello World"

我知道這種方法是無用的,因爲你可以設置sampleText的價值這樣

string sampleText = ""; 

但我只想做一些實驗,謝謝。

回答

1

我發現這樣ReadAsAsync<T>(this HttpContent content);在C#中的一些方法,我不知道什麼樣的方法是和我的頭上

這是一個泛型方法質疑彈出。您可以通過指定的類型調用它,你想是這樣的:

Foo result = await response.Content.ReadAsAsync<Foo>(); 

你可以閱讀更多關於它的MSDN:Generics

「這是可能創造這樣的「方法名(變量)方法{ }'這個方法是否存在?「

不,你想要做的是不可能的。 <...>之間是類型,而不是一個變量。

+0

所以這意味着'方法'只用於確定方法的類型? – 2014-10-09 01:07:18

+1

@LeoSarmiento'type'可以是方法中某些變量的類型,方法的任何參數/返回類型。此外,它可以用作某種通用方法的類型參數,從該方法調用。 – 2014-10-09 01:10:07

+0

@MadSorcerer現在我知道了,謝謝你的迴應。 – 2014-10-09 01:12:38

1

正如Thomas Levesque所說,ReadAsAsync<T>(this HttpContent content)是一種通用方法,可以使用不同的類型,具體取決於T類型參數。

因此,sampleText的值將變成「Hello World」。

如果這就是你要找的你應該使用ref的說法。

public void methodName(string text, ref string getText) 
{ 
    getText = text; 
} 

如何使用:

string sampleText; 
methodName("Hello World", ref sampleText); 
+0

很酷,我可以使用'ref'而不是'return'來設置變量的值,謝謝你的信息。 – 2014-10-09 01:17:34

+0

@LeoSarmiento是的,你可以,但要小心。儘可能使用'return'。 – 2014-10-09 01:42:04

1

什麼你看是Generic Method。它們用於重用代碼庫中包含的邏輯,並且您在這些尖括號之間看到的是所謂的Type Parameter

Type Parameters用於return指定的Type,或者用於指定參數的類型。

例如,讓我們說,我們希望得到一個名爲User

public IEnumerable<string> GetUserProperties() 
{ 
    return typeof(User).GetProperties().Select(property => property.Name); 
} 

public class User 
{ 
    public string UserId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

上面的代碼的問題類屬性的名稱時,我們不能重複使用其他類型的,說我們希望也得到一個Type命名School特性,我們會不斷創造新的方法來獲得任何給定的Type

public IEnumerable<string> GetSchoolProperties() 
{ 
    return typeof(School).GetProperties().Select(property => property.Name); 
} 

public class School 
{ 
    public string SchoolId { get; set; } 
    public string Name { get; set; } 
} 

的屬性要解決這個問題,我們美國EA Generic Method,即不限制到只有一個Type的方法(雖然限制可以適用於類型參數,它們的範圍爲分鐘之外,只是儘量包住你的頭腦解決此第一)

void Main() 
{ 
    User user = new User 
    { 
     FirstName = "Aydin", 
     LastName = "Aydin", 
     UserId = Guid.NewGuid().ToString() 
    }; 

    School school = new School 
    { 
     SchoolId = Guid.NewGuid().ToString(), 
     Name = "Aydins school" 
    }; 

    var userProperties = GetProperties(user); 
    var schoolProperties = GetProperties(school); 

    Console.WriteLine ("Retrieving the properties on the User class"); 
    foreach (var property in userProperties) 
    { 
     Console.WriteLine ("> {0}", property); 
    } 

    Console.WriteLine ("\nRetrieving the properties on the School class"); 
    foreach (var property in schoolProperties) 
    { 
     Console.WriteLine ("> {0}", property); 
    } 
} 

public static IEnumerable<string> GetProperties<T>(T t) 
{ 
    return t.GetType().GetProperties().Select(property => property.Name); 
} 

public class User 
{ 
    public string UserId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

public class School 
{ 
    public string SchoolId { get; set; } 
    public string Name { get; set; } 
}