Guest User

Untitled

a guest
Aug 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. Dynamic Constructor using Lambda and Func<>
  2. public class item
  3. {
  4. public string name { get; set; }
  5.  
  6. public item(string nam)
  7. {
  8. name = nam;
  9. }
  10.  
  11.  
  12. }
  13.  
  14. IList<string> alist = new string[] { "bob","mary"};
  15.  
  16. Func<string, item> func1 = x => new item(x);
  17.  
  18. Func<IEnumerable<string>,IList<item>> func2 = x=> x.ForEach(i => func1(i));
  19.  
  20. Func<IEnumerable<string>,IList<item>> func2 = x => x.Select( i => new item(i)).ToList();
  21.  
  22. IList<string> listOfStrings = new string[] { "bob","mary"};
  23. IList<item> listOfItems = listOfStrings.Select(s => new item(s)).ToList();
  24.  
  25. IEnumerable<item> myfunc(IEnumerable<string> stringlist)
  26. {
  27. var q = from s in stringlist
  28. select new item(s);
  29. return q.ToList();
  30. }
Add Comment
Please, Sign In to add comment