Advertisement
OedipusPrime

dynamic vs anonymous types

Aug 14th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             dynamic x = GetAThing();
  13.             Console.WriteLine(x.Title);
  14.  
  15.  
  16.             var myFoos = new[]
  17.                 {
  18.                     new Foo
  19.                         {
  20.                             Id = 1,
  21.                             Bar = "one",
  22.                             Baz = "eno"
  23.                         },
  24.                     new Foo
  25.                         {
  26.                             Id = 2,
  27.                             Bar = "two",
  28.                             Baz = "owt"
  29.                         }
  30.                 };
  31.  
  32.  
  33.             var myNewFoos = myFoos.Select(y => new {y.Bar, y.Baz});
  34.             foreach (var cur in myNewFoos)
  35.             {
  36.                 Console.WriteLine(cur.Bar, cur.Baz);
  37.             }
  38.  
  39.  
  40.             Console.ReadKey();
  41.         }
  42.  
  43.         public class Foo
  44.         {
  45.             public int Id { get; set; }
  46.             public string Bar { get; set; }
  47.             public string Baz { get; set; }
  48.         }
  49.  
  50.         static dynamic GetAThing()
  51.         {
  52.             dynamic foo = new ExpandoObject();
  53.             foo.Title = "Foo";
  54.             return foo;
  55.         }
  56.  
  57.         static void ProjectFoos(IEnumerable<Foo> foos)
  58.         {
  59.             var myNewFoos = foos.Select(y => new { y.Bar, y.Baz });
  60.             //return myNewFoos;
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement