Pass generic list as parameter

5

Is it possible to pass a generic list by parameter?

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

and I have my method:

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

How can I get my method to receive both List<Pessoa> and List<object> ?

    
asked by anonymous 29.04.2015 / 15:25

2 answers

11
  

This technique is called Generics no .net .

A simple way would look like this:

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

Where would you 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 output would be as follows:

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

But this 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 slightly cooler and more usual way you can use is to specify base types for Generics, which 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());
    }
}

You can 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 output would be this:

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
  

.NET Fiddle online sample.

    
29.04.2015 / 15:30
6

You can use Generics:

In one method the syntax is as follows:

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

For a class you use something like:

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

The cool thing is that you can work with interfaces and abstract 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: link , link

    
29.04.2015 / 15:38