Pass generic list as parameter

Asked

Viewed 8,444 times

6

You can pass a generic list by parameter?

Ex: I have one List<Pessoa> and a List<object>.

and I have my method:

public void FaçaAlgo(List<T> lista) { }

How can I make my method receive so much List<Pessoa> as List<object>?

2 answers

12


This technique is called Generics no. net.

A simple way would be like this:

// especificando o T na assinatura do método
public void FacaAlgo<T>(List<T> lista){}

Where you could use it like this, for example:

Assuming the method does this:

public void FacaAlgo<T>(List<T> lista)
{
    foreach (T t in lista)
    {
        Console.WriteLine(t.ToString());
    }
}

You can use the method this way:

List<string> listString = new List<string>();
listString.Add("String 1");
listString.Add("String 2");
listString.Add("String 3");
listString.Add("String 4");
FacaAlgo(listString);

List<object> listObject = new List<object>();
listObject.Add("Object 1");
listObject.Add("Object 2");
listObject.Add("Object 3");
listObject.Add("Object 4");
FacaAlgo(listObject);

Where the exit would be next:

String 1
String 2
String 3
String 4
Object 1
Object 2
Object 3
Object 4

But that would be the simple way, there are other ways, where you can specify the type in the instance of class:

public class MyClass<T>
{
        public void FacaAlgo(List<T> lista)
        {
            // ...
        }
    // exemplo de utilização:
    // new MyClass<string>().FacaAlgo(listString);
}

And another way a little more cool and usual that you can use, is to specify basic types for Generics, can be class or interfaces.

Where given the following class structure:

public class TipoBase
{
    public int Value { get; set; }

    public virtual string PrintValue()
    {
        return "O valor na class TipoBase é: " + Value;
    }
}

public class Tipo1 : TipoBase
{
    public override string PrintValue()
    {
        return "O valor na class Tipo1 é: " + Value;
    }
}

public class Tipo2 : TipoBase
{
    public override string PrintValue()
    {
        return "O valor na class Tipo2 é: " + Value;
    }
}

We can implement the following method:

public void FacaAlgoComTipoBase<T>(List<T> lista) where T : TipoBase
{
    foreach (T t in lista)
    {
        // assim eu posso chamar o método PrintValue(), garantido pela class TipoBase
        Console.WriteLine(t.PrintValue());
    }
}

It is possible to use it as follows, for example:

List<TipoBase> listTipoBase = new List<TipoBase>(); 
listTipoBase.Add(new TipoBase(){Value = 1});
listTipoBase.Add(new TipoBase() { Value = 2 });

List<Tipo1> listTipo1 = new List<Tipo1>();
listTipoBase.Add(new Tipo1() { Value = 1 });
listTipoBase.Add(new Tipo1() { Value = 2 });

List<Tipo2> listTipo2 = new List<Tipo2>();
listTipoBase.Add(new Tipo2() { Value = 1 });
listTipoBase.Add(new Tipo2() { Value = 2 });

FacaAlgoComTipoBase(listTipoBase);
FacaAlgoComTipoBase(listTipo1);
FacaAlgoComTipoBase(listTipo2);

Where the exit would be:

O valor na class TipoBase é: 1
O valor na class TipoBase é: 2
O valor na class Tipo1 é: 1
O valor na class Tipo1 é: 2
O valor na class Tipo2 é: 1
O valor na class Tipo2 é: 2

Online example . NET Fiddle.

7

You can use Generics:

In a method the syntax is the following:

public void FacaAlgo<T>(List<T> lista){}

For a Voce class uses something like:

public class Classe<T>
{
    public void FacaAlgo(List<T> lista)
}

The cool thing is that you can work with abstract interfaces and classes to define contracts, for example:

public inteface IParaFazer
{
    void Executar();
}

public classe Classe2<T> where T : IParaFazer
{
    public void FacaAlgo(List<T> lista)
    {
        foreach(IParaFazer obj in lista)
        {
            obj.Executar();
        } 
    }
}

Take a look at the . NET documentation: https://msdn.microsoft.com/pt-br/library/512aeb7t.aspx, https://msdn.microsoft.com/pt-br/library/0x6a29h6.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.