Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication1 {
  9. class Program {
  10. static void Main(string[] args) {
  11. Car car1 = new Car("Ferrari", "FF");
  12. Car car2 = new Car("Mercedes Benz", "S63 Coupe");
  13. Car car3 = new Car("Land Rover", "Range Rover Sport SVR");
  14. Car car4 = new Car("Land Rover", "Range Rover Autobiography");
  15. Car car5 = new Car("Land Rover", "Defender");
  16. Car car6 = new Car("Land Rover", "Range Rover Vogue");
  17.  
  18. // ArrayList
  19. ArrayList myArrayList = new ArrayList();
  20.  
  21. myArrayList.Add(car1);
  22. myArrayList.Add(car2);
  23. myArrayList.Add(car3);
  24.  
  25. foreach (Car car in myArrayList) {
  26. Console.WriteLine(car.Model);
  27. }
  28.  
  29. // Type list
  30. List<Car> myList = new List<Car>();
  31.  
  32. myList.Add(car1);
  33. myList.Add(car2);
  34. myList.Add(car3);
  35. myList.Add(car4);
  36. myList.Add(car5);
  37. myList.Add(car6);
  38.  
  39. List<Car> dailyDrivers = new List<Car>() {
  40. new Car { Maker = "Bentley", Model = "Continental GT" },
  41. new Car { Maker = "Rolls-Royce", Model = "Ghost" }
  42. };
  43.  
  44. foreach (Car car in dailyDrivers) {
  45. Console.WriteLine(car.Maker);
  46. }
  47.  
  48. // Dictionary
  49. Dictionary<string, Car> myDictionary = new Dictionary<string, Car>();
  50.  
  51. myDictionary.Add(car1.Maker, car1);
  52. myDictionary.Add(car2.Maker, car2);
  53.  
  54. Console.WriteLine(myDictionary["Ferrari"].Model);
  55.  
  56. // LINQ crap
  57. var queryResults = from car in myList
  58. where car.Maker == "Ferrari"
  59. select car;
  60.  
  61. foreach (var result in queryResults) {
  62. Console.WriteLine(result.ToString());
  63. }
  64.  
  65. // More LINQ crap
  66. var methodResults = myList.Where(p => p.Maker == "Land Rover");
  67.  
  68. Console.WriteLine("\n\nQuery \"{0}\"")
  69. foreach (var result in methodResults) {
  70. Console.WriteLine(result.ToString());
  71. }
  72.  
  73. Console.ReadKey();
  74. }
  75. }
  76.  
  77. class Car {
  78. public string Maker { get; set; }
  79. public string Model { get; set; }
  80.  
  81. public Car () {
  82. Maker = string.Empty;
  83. Model = string.Empty;
  84. }
  85.  
  86. public Car (string maker) {
  87. Maker = maker;
  88. }
  89.  
  90. public Car (string maker, string model) {
  91. Maker = maker;
  92. Model = model;
  93. }
  94.  
  95. public override string ToString () {
  96. return Maker + " " + Model;
  97. }
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement