Onel Sarmiento
Onel Sarmiento

Reputation: 1626

Creating new method c# methodname<Variable>(Variable)

I found some method like this ReadAsAsync<T>(this HttpContent content); in c# and I don't know what kind of method is that and question popup on my head

"It is possible to create method like this 'methodName<varible>(variable){}' and does this method exist somewhere?"

For example:

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

And when I call the method:

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

So the value of the sampleText will become "Hello World".

I know this kind of method is useless because you can set the value of sampleText like this

string sampleText = "";

But I Just want to make some experiment, Thank You.

Upvotes: 0

Views: 118

Answers (3)

Aydin
Aydin

Reputation: 15314

What you're looking at is a Generic Method. They're used to reuse the logic contained in your code base, and what you see in between those angle brackets is what's called a Type Parameter.

Type Parameters are used to either return the specified Type, or, to specify the type of a parameter.

For instance, let's say that we want to get the names of the properties on a class called 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; }
}

The problem with the code above is that we can't reuse it for other types, say that we wanted to also get the properties of a Type named School, we would be constantly creating new methods to get the properties of any given 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; }
}

To solve this issue, we use a Generic Method, a method that isn't constrained to just one Type (although constraints can be applied to Type parameters, they're outside of scope for the minute, just try to wrap your mind around this first)

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; }
}

Upvotes: 1

Diligent Key Presser
Diligent Key Presser

Reputation: 4263

As Thomas Levesque said, ReadAsAsync<T>(this HttpContent content) is a generic method, wich can operate with different types, depending on T Type parameter.

So the value of the sampleText will become "Hello World".

if that is what you are looking for you should use ref argument.

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

How to use:

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

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292685

I found some method like this ReadAsAsync<T>(this HttpContent content); in c# and I don't know what kind of method is that and question popup on my head

That's a generic method. You call it by specifying the type you want like this:

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

You can read more about it on MSDN: Generics

"It is possible to create method like this 'methodName(variable){}' and does this method exist somewhere?"

No, what you're trying to do is not possible. What goes between the <...> is a type, not a variable.

Upvotes: 1

Related Questions