Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. _____ _ _ _ _
  2. / __ \ | | | | | (_)
  3. | / \/ ___ | | | ___ ___| |_ _ ___ _ __ ___
  4. | | / _ \| | |/ _ \/ __| __| |/ _ \| '_ \/ __|
  5. | \__/\ (_) | | | __/ (__| |_| | (_) | | | \__ \
  6. \____/\___/|_|_|\___|\___|\__|_|\___/|_| |_|___/
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. PreRequisite
  14. Virtual
  15. Arrays
  16.  
  17.  
  18.  
  19. Literal meaning :A group of things
  20.  
  21. In c# there are two types of collections
  22. >its mostly about what kinds of things they are storing
  23. ->Non Generic
  24. They are like your knapsack or purse that you carry around
  25. So they all provides storage for you but at the same time dosent constraint to store only a particular type of object
  26. Like in your knapsack you can store your laptop at the same time you can keep your notebook so basically anything that pretty much "fits in" your knapsack
  27.  
  28. But thats just an anolgy in the world of c# there is a little catch to the entire phenomenon
  29. The ability to store anything is achived by boxing and unboxing{citation needed}
  30. So the elements to be stored in such collections are boxed to object and later they have to be manually unboxed
  31. They are called simply collections in C#
  32. ->Generic
  33. Consider them to be like a box of candy that i gave jewel after i made her cry :( //am not proud of it
  34. So one thing you must have noticed that although it was a box but it contains only one kind of candy
  35. so these boxes are specifically customized to hold the intended objects that makes them efficient as they are not expecting anything else but just choclates
  36. So in the world of c# to have these kind of classes you need to tell what kind of objects are you looking to hold theough the angular thingy "<>" so the resultant "Box" or the generic collection objects will be specifically designed to hold them there by they will be more efficient
  37. they are called "Generic collections"
  38.  
  39. \ | | (_) | | | | | (_)
  40. | \| | ___ _ __ __ _ ___ _ __ ___ _ __ _ ___ ___ ___ | | | ___ ___| |_ _ ___ _ __
  41. | . ` |/ _ \| '_ \ / _` |/ _ \ '_ \ / _ \ '__| |/ __| / __/ _ \| | |/ _ \/ __| __| |/ _ \| '_ \
  42. | |\ | (_) | | | | | (_| | __/ | | | __/ | | | (__ | (_| (_) | | | __/ (__| |_| | (_) | | | |
  43. |_| \_|\___/|_| |_| \__, |\___|_| |_|\___|_| |_|\___| \___\___/|_|_|\___|\___|\__|_|\___/|_| |_|
  44. __/ |
  45. |___/
  46.  
  47. In C#:Collection classes are specialized classes for data storage and retrieval .
  48. There are several types of non generic collections
  49. Few of the important once are as follow
  50. ->ArrayList
  51. ->HashTables
  52. ->Stack
  53. ->Queue
  54. ->BitArray
  55.  
  56. _ _ _
  57. /\ | | (_) | |
  58. / \ _ __ _ __ __ _ _ _| | _ ___| |_
  59. / /\ \ | '__| '__/ _` | | | | | | / __| __|
  60. / ____ \| | | | | (_| | |_| | |____| \__ \ |_
  61. /_/ \_\_| |_| \__,_|\__, |______|_|___/\__|
  62. __/ |
  63. |___/
  64. "It represents an ordered collection of an object that can be indexed individually."
  65. just another fancy way of saying you can do things like this ->arraylistobj[1]
  66. Can be considered as an alternative to an array
  67. Then a Million $ question will be why not to just use an array
  68. Well there are several advantages as i spoke earlier
  69. >For one they are "dynamic"
  70. Means they can expand and shrink as you enter to delete elements from them something that arrays are not fundamentally capable of{citation required cover the object corner case}
  71. >Next they give you tons of already implemented built in properties and methods that comes in handy {citation required refer previous tutorial //spice ex}again arrays dosent give us that
  72.  
  73. Property Description
  74. ------------------------------------------------------------------------------------
  75. |Capacity | Gets or sets the number of elements that the ArrayList can contain. |
  76. |Count | Gets the number of elements actually contained in the ArrayList. |
  77. |IsFixedSize |Gets a value indicating whether the ArrayList has a fixed size. |
  78. |IsReadOnly | Gets a value indicating whether the ArrayList is read-only. |
  79. |Item | Gets or sets the element at the specified index. |
  80. ------------------------------------------------------------------------------------
  81.  
  82. {citation required object type and virtual}
  83. 1 public virtual int Add(object value);
  84. Adds an object to the end of the ArrayList.
  85.  
  86. 2 public virtual void AddRange(ICollection c);
  87. Adds the elements of an ICollection to the end of the ArrayList.
  88.  
  89. 3 public virtual void Clear();
  90. Removes all elements from the ArrayList.
  91.  
  92. 4 public virtual bool Contains(object item);
  93. Determines whether an element is in the ArrayList.
  94.  
  95. 5 public virtual ArrayList GetRange(int index, int count);
  96. Returns an ArrayList which represents a subset of the elements in the source ArrayList.
  97.  
  98. 6 public virtual int IndexOf(object);
  99. Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
  100.  
  101. 7 public virtual void Insert(int index, object value);
  102. Inserts an element into the ArrayList at the specified index.
  103.  
  104. 8 public virtual void InsertRange(int index, ICollection c);
  105. Inserts the elements of a collection into the ArrayList at the specified index.
  106.  
  107. 9 public virtual void Remove(object obj);
  108. Removes the first occurrence of a specific object from the ArrayList.
  109.  
  110. 10 public virtual void RemoveAt(int index);
  111. Removes the element at the specified index of the ArrayList.
  112.  
  113. 11 public virtual void RemoveRange(int index, int count);
  114. Removes a range of elements from the ArrayList.
  115.  
  116. 12 public virtual void Reverse();
  117. Reverses the order of the elements in the ArrayList.
  118.  
  119. 13 public virtual void SetRange(int index, ICollection c);
  120. Copies the elements of a collection over a range of elements in the ArrayList.
  121.  
  122. 14 public virtual void Sort();
  123. Sorts the elements in the ArrayList.
  124.  
  125. 15 public virtual void TrimToSize();
  126. Sets the capacity to the actual number of elements in the ArrayList.
  127.  
  128.  
  129. Example
  130.  
  131. {citation required show her what happens when you add another type to it and project the downside of non generic collecion}
  132. namespace CollectionApplication
  133. {
  134. class Program
  135. {
  136. static void Main(string[] args)
  137. {
  138. ArrayList al = new ArrayList();
  139.  
  140. Console.WriteLine("Adding some numbers:");
  141. al.Add(45);
  142. al.Add(78);
  143. al.Add(33);
  144. al.Add(56);
  145. al.Add(12);
  146. al.Add(23);
  147. al.Add(9);
  148.  
  149. Console.WriteLine("Capacity: {0} ", al.Capacity);
  150. Console.WriteLine("Count: {0}", al.Count);
  151.  
  152. Console.Write("Content: ");
  153. foreach (int i in al)
  154. {
  155. Console.Write(i + " ");
  156. }
  157.  
  158. Console.WriteLine();
  159. Console.Write("Sorted Content: ");
  160. al.Sort();
  161. foreach (int i in al)
  162. {
  163. Console.Write(i + " ");
  164. }
  165. Console.WriteLine();
  166. Console.ReadKey();
  167. }
  168. }
  169. }
  170.  
  171.  
  172. Add range example
  173.  
  174. using System;
  175. using System.Collections;
  176.  
  177. class Program
  178. {
  179. static void Main()
  180. {
  181. //
  182. // Create an ArrayList with two values.
  183. //
  184. ArrayList list = new ArrayList();
  185. list.Add(5);
  186. list.Add(7);
  187. //
  188. // Second ArrayList.
  189. //
  190. ArrayList list2 = new ArrayList();
  191. list2.Add(10);
  192. list2.Add(13);
  193. //
  194. // Add second ArrayList to first.
  195. //
  196. list.AddRange(list2);
  197. //
  198. // Display the values.
  199. //
  200. foreach (int i in list)
  201. {
  202. Console.WriteLine(i);
  203. }
  204. }
  205. }
  206.  
  207.  
  208. Trim to size example
  209. using System;
  210. using System.Collections;
  211. public class SamplesArrayList {
  212.  
  213. public static void Main() {
  214.  
  215. // Creates and initializes a new ArrayList.
  216. ArrayList myAL = new ArrayList();
  217. myAL.Add( "The" );
  218. myAL.Add( "quick" );
  219. myAL.Add( "brown" );
  220. myAL.Add( "fox" );
  221. myAL.Add( "jumped" );
  222.  
  223. // Displays the count, capacity and values of the ArrayList.
  224. Console.WriteLine( "Initially," );
  225. Console.WriteLine( " Count : {0}", myAL.Count );
  226. Console.WriteLine( " Capacity : {0}", myAL.Capacity );
  227. Console.Write( " Values:" );
  228. PrintValues( myAL );
  229.  
  230. // Trim the ArrayList.
  231. myAL.TrimToSize();
  232.  
  233. // Displays the count, capacity and values of the ArrayList.
  234. Console.WriteLine( "After TrimToSize," );
  235. Console.WriteLine( " Count : {0}", myAL.Count );
  236. Console.WriteLine( " Capacity : {0}", myAL.Capacity );
  237. Console.Write( " Values:" );
  238. PrintValues( myAL );
  239.  
  240. // Clear the ArrayList.
  241. myAL.Clear();
  242.  
  243. // Displays the count, capacity and values of the ArrayList.
  244. Console.WriteLine( "After Clear," );
  245. Console.WriteLine( " Count : {0}", myAL.Count );
  246. Console.WriteLine( " Capacity : {0}", myAL.Capacity );
  247. Console.Write( " Values:" );
  248. PrintValues( myAL );
  249.  
  250. // Trim the ArrayList again.
  251. myAL.TrimToSize();
  252.  
  253. // Displays the count, capacity and values of the ArrayList.
  254. Console.WriteLine( "After the second TrimToSize," );
  255. Console.WriteLine( " Count : {0}", myAL.Count );
  256. Console.WriteLine( " Capacity : {0}", myAL.Capacity );
  257. Console.Write( " Values:" );
  258. PrintValues( myAL );
  259. }
  260.  
  261. public static void PrintValues( IEnumerable myList ) {
  262. foreach ( Object obj in myList )
  263. Console.Write( " {0}", obj );
  264. Console.WriteLine();
  265. }
  266.  
  267. }
  268.  
  269. _____ _ _
  270. / ____| | | |
  271. | (___ | |_ __ _ ___| | __
  272. \___ \| __/ _` |/ __| |/ /
  273. ____) | || (_| | (__| <
  274. |_____/ \__\__,_|\___|_|\_\
  275.  
  276.  
  277. >It represents a last-in, first out collection of object.
  278. >It is used when you need a last-in, first-out access of items.
  279. >When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
  280. -----------------------------------------------------------------
  281. |Property |Description |
  282. |Count |Gets the number of elements contained in the Stack.|
  283. -----------------------------------------------------------------
  284.  
  285.  
  286. 1 public virtual void Clear();
  287. Removes all elements from the Stack.
  288.  
  289. 2 public virtual bool Contains(object obj);
  290. Determines whether an element is in the Stack.
  291.  
  292. 3 public virtual object Peek();
  293. Returns the object at the top of the Stack without removing it.
  294.  
  295. 4 public virtual object Pop();
  296. Removes and returns the object at the top of the Stack.
  297.  
  298. 5 public virtual void Push(object obj);
  299. Inserts an object at the top of the Stack.
  300.  
  301. 6 public virtual object[] ToArray();
  302. Copies the Stack to a new array.
  303.  
  304.  
  305.  
  306. Example
  307. using System;
  308. using System.Collections;
  309.  
  310. namespace CollectionsApplication
  311. {
  312. class Program
  313. {
  314. static void Main(string[] args)
  315. {
  316. Stack st = new Stack();
  317.  
  318. st.Push('A');
  319. st.Push('M');
  320. st.Push('G');
  321. st.Push('W');
  322.  
  323. Console.WriteLine("Current stack: ");
  324. foreach (char c in st)
  325. {
  326. Console.Write(c + " ");
  327. }
  328.  
  329. Console.WriteLine();
  330.  
  331. st.Push('V');
  332. st.Push('H');
  333. Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
  334. Console.WriteLine("Current stack: ");
  335. foreach (char c in st)
  336. {
  337. Console.Write(c + " ");
  338. }
  339.  
  340. Console.WriteLine();
  341.  
  342. Console.WriteLine("Removing values ");
  343. st.Pop();
  344. st.Pop();
  345. st.Pop();
  346.  
  347. Console.WriteLine("Current stack: ");
  348. foreach (char c in st)
  349. {
  350. Console.Write(c + " ");
  351. }
  352. }
  353. }
  354. }
  355. Ex: 2
  356. using System;
  357. using System.Collections;
  358. public class SamplesStack {
  359.  
  360. public static void Main() {
  361.  
  362. // Creates and initializes a new Stack.
  363. Stack myStack = new Stack();
  364. myStack.Push( "The" );
  365. myStack.Push( "quick" );
  366. myStack.Push( "brown" );
  367. myStack.Push( "fox" );
  368.  
  369. // Displays the Stack.
  370. Console.Write( "Stack values:" );
  371. PrintValues( myStack, '\t' );
  372.  
  373. // Removes an element from the Stack.
  374. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
  375.  
  376. // Displays the Stack.
  377. Console.Write( "Stack values:" );
  378. PrintValues( myStack, '\t' );
  379.  
  380. // Removes another element from the Stack.
  381. Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
  382.  
  383. // Displays the Stack.
  384. Console.Write( "Stack values:" );
  385. PrintValues( myStack, '\t' );
  386.  
  387. // Views the first element in the Stack but does not remove it.
  388. Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );
  389.  
  390. // Displays the Stack.
  391. Console.Write( "Stack values:" );
  392. PrintValues( myStack, '\t' );
  393. }
  394.  
  395.  
  396. public static void PrintValues( IEnumerable myCollection, char mySeparator ) {
  397. foreach ( Object obj in myCollection )
  398. Console.Write( "{0}{1}", mySeparator, obj );
  399. Console.WriteLine();
  400. }
  401.  
  402. }
  403.  
  404.  
  405. Generic collection
  406.  
  407. Explain array to her
  408. Interfaces they implement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement