Simple Collection in C#

Collection classes are specialized classes for data storage and retrieval.

These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.

Create and manage group of related object , Then there are two way to handle this first is array and second one is collection array are most useful for fixed and strongly type object.

Collection provide more flexible to work with group of object, group of object can shrink and grow dynamically.

for some collection when we need to assign a key to object then put into collection and retrive data using key.

A collection is a class, so you must declare a new collection before you can add elements to that

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  

  6. namespace Collection  
  7. {  
  8.     class Program  
  9.     {  
  10.       /* 
  11.       Collection classes are specialized classes for data storage and retrieval. 
  12.        * These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces. 
  13. */  
  14.      //*************************** Simple Collection ***************************  
  15.           //create and manage group of related object , Then there are two way to handle this first is array and second one is collection  
  16. // array are most useful for fixed and strongly type object .  
  17. // Collection provide more flexible to work with group of object, group of object can shrink and grow dynamically  
  18. // for some collection when we need to assign a key to object then put into collection and retrive data using key  
  19. //A collection is a class, so you must declare a new collection before you can add elements to that  
  20. static void Main(string[] args)  
  21. {  
  22. var sal = new List<string> { "I""M""collection" }; // anonymos  
  23. sal.Add("Manu"); // add value  
  24. sal.Add("Gupta");  
  25. foreach (var a in sal)  
  26. {  
  27.     // Iterate value from list  
  28.     Console.WriteLine("Data is " + a);  
  29. }  
  30. Cat cat = new Cat { Age = 10, Name = "Fluffy" }; { }  
  31. List<Cat> cats = new List<Cat> // Generic (string and int) used for type safety and type casting  
  32. {  
  33.     new Cat(){ Name = "US", Age=8 },  
  34.     new Cat(){ Name = "Delhi", Age=2 },  
  35.     new Cat(){ Name = "London", Age=14 }  
  36. };  
  37. Console.WriteLine("Generic is ...........");  
  38. foreach (Cat c in cats)  
  39. {  
  40.     Console.WriteLine("Generic is " + c);  
  41. }  
  42. var salint = new List<int>();// strongly type  
  43. salint.Add(1);  
  44. salint.Add(2);  
  45. foreach (var b in salint)  
  46. {  
  47.     Console.WriteLine("Data is " + b);  
  48. }  
  49. salint.Remove(0); // remove element from 0 index  
  50. var obj = new List<string> { "Faizabad""Patna""Varanasi" };  
  51. for (var index = 0; index < obj.Count;index++ )  
  52. {  
  53.     Console.WriteLine("In for loop " +obj[index] );  
  54. }  
  55. var theGalaxies = new List<Galaxy> // Galaxy is class  
  56. {  
  57.     new Galaxy { Name="Tadpole", MegaLightYears=400},  
  58.     new Galaxy { Name="Pinwheel", MegaLightYears=25},  
  59.     new Galaxy { Name="Milky Way", MegaLightYears=0},  
  60.     new Galaxy { Name="Andromeda", MegaLightYears=3}  
  61. };  
  62. foreach (Galaxy theGalaxy in theGalaxies)  
  63. {  
  64.     Console.WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears);  
  65. }  
  66.    Console.ReadKey(true);  
  67. }  
  68. class Cat  
  69. {  
  70.     public int Age { getset; }  
  71.     public string Name { getset; }  
  72. }  
  73. public class Galaxy  
  74. {  
  75.     public string Name { getset; }  
  76.     public int MegaLightYears { getset; }