正在创建新方法c#methodname<;变量>;(可变)

本文关键字:变量 gt 可变 lt 创建 新方法 c#methodname | 更新日期: 2023-09-27 18:26:03

我在c#中找到了一些类似ReadAsAsync<T>(this HttpContent content);的方法,我不知道这是什么样的方法,问题在我的头上弹出

"可以创建这样的方法'methodName<varible>(variable){}',这种方法是否存在?"

例如:

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

当我调用方法时:

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

因此,sampleText的值将变为"Hello World"

我知道这种方法是无用的,因为你可以像一样设置sampleText的值

string sampleText = "";

但我只是想做一些实验,谢谢。

正在创建新方法c#methodname<;变量>;(可变)

我在c#中发现了一些类似ReadAsAsync<T>(this HttpContent content);的方法,我不知道这是什么样的方法,问题在我的头上弹出

这是一个通用方法。你可以这样指定你想要的类型来调用它:

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

你可以在MSDN上阅读更多关于它的信息:Generics

"有可能创建这样的方法‘methodName(变量){}’,这个方法是否存在?"

不,你想做的是不可能的。介于<...>之间的是类型,而不是变量。

正如Thomas Levsque所说,ReadAsAsync<T>(this HttpContent content)是一种通用方法,可以根据T Type参数使用不同的类型进行操作。

因此sampleText的值将变为"Hello World"。

如果这就是您想要的,您应该使用ref参数。

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

如何使用:

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

您看到的是一个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; }
}

上面代码的问题是,我们不能将其用于其他类型,比如说,我们也想获得名为SchoolType的属性,我们会不断创建新方法来获得任何给定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; }
}

为了解决这个问题,我们使用了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; }
}