fork download
  1. using static System.Console;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program {
  6. public static void Main() {
  7. var lista = ListarAnonimo();
  8. foreach (var item in lista) {
  9. var pessoa = Util.Cast(item, new { Nome = "", Idade = 0, Salario = 0.0m });
  10. WriteLine($"Nome: {pessoa.Nome} - Idade: {pessoa.Idade} - Salario {pessoa.Salario}");
  11. }
  12. }
  13. public static List<object> ListarAnonimo() {
  14. var lista = new object[] {
  15. new {
  16. Nome = "",
  17. Idade = 0,
  18. Salario = 0.0m
  19. }
  20. }.ToList();
  21. lista.Clear();
  22. lista.Add(new {
  23. Nome = "Gato",
  24. Idade = 25,
  25. Salario = 3000000.0m
  26. });
  27. return lista;
  28. }
  29. }
  30.  
  31. public static class Util {
  32. public static T Cast<T>(object obj, T type) => (T)obj;
  33. }
  34.  
  35. //https://pt.stackoverflow.com/q/183594/101
Success #stdin #stdout 0.02s 17392KB
stdin
Standard input is empty
stdout
Nome: Gato - Idade: 25 - Salario 3000000.0