Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _____ _ _ _ _
- / __ \ | | | | | (_)
- | / \/ ___ | | | ___ ___| |_ _ ___ _ __ ___
- | | / _ \| | |/ _ \/ __| __| |/ _ \| '_ \/ __|
- | \__/\ (_) | | | __/ (__| |_| | (_) | | | \__ \
- \____/\___/|_|_|\___|\___|\__|_|\___/|_| |_|___/
- PreRequisite
- Virtual
- Arrays
- Literal meaning :A group of things
- In c# there are two types of collections
- >its mostly about what kinds of things they are storing
- ->Non Generic
- They are like your knapsack or purse that you carry around
- So they all provides storage for you but at the same time dosent constraint to store only a particular type of object
- 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
- But thats just an anolgy in the world of c# there is a little catch to the entire phenomenon
- The ability to store anything is achived by boxing and unboxing{citation needed}
- So the elements to be stored in such collections are boxed to object and later they have to be manually unboxed
- They are called simply collections in C#
- ->Generic
- Consider them to be like a box of candy that i gave jewel after i made her cry :( //am not proud of it
- So one thing you must have noticed that although it was a box but it contains only one kind of candy
- 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
- 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
- they are called "Generic collections"
- \ | | (_) | | | | | (_)
- | \| | ___ _ __ __ _ ___ _ __ ___ _ __ _ ___ ___ ___ | | | ___ ___| |_ _ ___ _ __
- | . ` |/ _ \| '_ \ / _` |/ _ \ '_ \ / _ \ '__| |/ __| / __/ _ \| | |/ _ \/ __| __| |/ _ \| '_ \
- | |\ | (_) | | | | | (_| | __/ | | | __/ | | | (__ | (_| (_) | | | __/ (__| |_| | (_) | | | |
- |_| \_|\___/|_| |_| \__, |\___|_| |_|\___|_| |_|\___| \___\___/|_|_|\___|\___|\__|_|\___/|_| |_|
- __/ |
- |___/
- In C#:Collection classes are specialized classes for data storage and retrieval .
- There are several types of non generic collections
- Few of the important once are as follow
- ->ArrayList
- ->HashTables
- ->Stack
- ->Queue
- ->BitArray
- _ _ _
- /\ | | (_) | |
- / \ _ __ _ __ __ _ _ _| | _ ___| |_
- / /\ \ | '__| '__/ _` | | | | | | / __| __|
- / ____ \| | | | | (_| | |_| | |____| \__ \ |_
- /_/ \_\_| |_| \__,_|\__, |______|_|___/\__|
- __/ |
- |___/
- "It represents an ordered collection of an object that can be indexed individually."
- just another fancy way of saying you can do things like this ->arraylistobj[1]
- Can be considered as an alternative to an array
- Then a Million $ question will be why not to just use an array
- Well there are several advantages as i spoke earlier
- >For one they are "dynamic"
- 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}
- >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
- Property Description
- ------------------------------------------------------------------------------------
- |Capacity | Gets or sets the number of elements that the ArrayList can contain. |
- |Count | Gets the number of elements actually contained in the ArrayList. |
- |IsFixedSize |Gets a value indicating whether the ArrayList has a fixed size. |
- |IsReadOnly | Gets a value indicating whether the ArrayList is read-only. |
- |Item | Gets or sets the element at the specified index. |
- ------------------------------------------------------------------------------------
- {citation required object type and virtual}
- 1 public virtual int Add(object value);
- Adds an object to the end of the ArrayList.
- 2 public virtual void AddRange(ICollection c);
- Adds the elements of an ICollection to the end of the ArrayList.
- 3 public virtual void Clear();
- Removes all elements from the ArrayList.
- 4 public virtual bool Contains(object item);
- Determines whether an element is in the ArrayList.
- 5 public virtual ArrayList GetRange(int index, int count);
- Returns an ArrayList which represents a subset of the elements in the source ArrayList.
- 6 public virtual int IndexOf(object);
- Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
- 7 public virtual void Insert(int index, object value);
- Inserts an element into the ArrayList at the specified index.
- 8 public virtual void InsertRange(int index, ICollection c);
- Inserts the elements of a collection into the ArrayList at the specified index.
- 9 public virtual void Remove(object obj);
- Removes the first occurrence of a specific object from the ArrayList.
- 10 public virtual void RemoveAt(int index);
- Removes the element at the specified index of the ArrayList.
- 11 public virtual void RemoveRange(int index, int count);
- Removes a range of elements from the ArrayList.
- 12 public virtual void Reverse();
- Reverses the order of the elements in the ArrayList.
- 13 public virtual void SetRange(int index, ICollection c);
- Copies the elements of a collection over a range of elements in the ArrayList.
- 14 public virtual void Sort();
- Sorts the elements in the ArrayList.
- 15 public virtual void TrimToSize();
- Sets the capacity to the actual number of elements in the ArrayList.
- Example
- {citation required show her what happens when you add another type to it and project the downside of non generic collecion}
- namespace CollectionApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList al = new ArrayList();
- Console.WriteLine("Adding some numbers:");
- al.Add(45);
- al.Add(78);
- al.Add(33);
- al.Add(56);
- al.Add(12);
- al.Add(23);
- al.Add(9);
- Console.WriteLine("Capacity: {0} ", al.Capacity);
- Console.WriteLine("Count: {0}", al.Count);
- Console.Write("Content: ");
- foreach (int i in al)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine();
- Console.Write("Sorted Content: ");
- al.Sort();
- foreach (int i in al)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- }
- Add range example
- using System;
- using System.Collections;
- class Program
- {
- static void Main()
- {
- //
- // Create an ArrayList with two values.
- //
- ArrayList list = new ArrayList();
- list.Add(5);
- list.Add(7);
- //
- // Second ArrayList.
- //
- ArrayList list2 = new ArrayList();
- list2.Add(10);
- list2.Add(13);
- //
- // Add second ArrayList to first.
- //
- list.AddRange(list2);
- //
- // Display the values.
- //
- foreach (int i in list)
- {
- Console.WriteLine(i);
- }
- }
- }
- Trim to size example
- using System;
- using System.Collections;
- public class SamplesArrayList {
- public static void Main() {
- // Creates and initializes a new ArrayList.
- ArrayList myAL = new ArrayList();
- myAL.Add( "The" );
- myAL.Add( "quick" );
- myAL.Add( "brown" );
- myAL.Add( "fox" );
- myAL.Add( "jumped" );
- // Displays the count, capacity and values of the ArrayList.
- Console.WriteLine( "Initially," );
- Console.WriteLine( " Count : {0}", myAL.Count );
- Console.WriteLine( " Capacity : {0}", myAL.Capacity );
- Console.Write( " Values:" );
- PrintValues( myAL );
- // Trim the ArrayList.
- myAL.TrimToSize();
- // Displays the count, capacity and values of the ArrayList.
- Console.WriteLine( "After TrimToSize," );
- Console.WriteLine( " Count : {0}", myAL.Count );
- Console.WriteLine( " Capacity : {0}", myAL.Capacity );
- Console.Write( " Values:" );
- PrintValues( myAL );
- // Clear the ArrayList.
- myAL.Clear();
- // Displays the count, capacity and values of the ArrayList.
- Console.WriteLine( "After Clear," );
- Console.WriteLine( " Count : {0}", myAL.Count );
- Console.WriteLine( " Capacity : {0}", myAL.Capacity );
- Console.Write( " Values:" );
- PrintValues( myAL );
- // Trim the ArrayList again.
- myAL.TrimToSize();
- // Displays the count, capacity and values of the ArrayList.
- Console.WriteLine( "After the second TrimToSize," );
- Console.WriteLine( " Count : {0}", myAL.Count );
- Console.WriteLine( " Capacity : {0}", myAL.Capacity );
- Console.Write( " Values:" );
- PrintValues( myAL );
- }
- public static void PrintValues( IEnumerable myList ) {
- foreach ( Object obj in myList )
- Console.Write( " {0}", obj );
- Console.WriteLine();
- }
- }
- _____ _ _
- / ____| | | |
- | (___ | |_ __ _ ___| | __
- \___ \| __/ _` |/ __| |/ /
- ____) | || (_| | (__| <
- |_____/ \__\__,_|\___|_|\_\
- >It represents a last-in, first out collection of object.
- >It is used when you need a last-in, first-out access of items.
- >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.
- -----------------------------------------------------------------
- |Property |Description |
- |Count |Gets the number of elements contained in the Stack.|
- -----------------------------------------------------------------
- 1 public virtual void Clear();
- Removes all elements from the Stack.
- 2 public virtual bool Contains(object obj);
- Determines whether an element is in the Stack.
- 3 public virtual object Peek();
- Returns the object at the top of the Stack without removing it.
- 4 public virtual object Pop();
- Removes and returns the object at the top of the Stack.
- 5 public virtual void Push(object obj);
- Inserts an object at the top of the Stack.
- 6 public virtual object[] ToArray();
- Copies the Stack to a new array.
- Example
- using System;
- using System.Collections;
- namespace CollectionsApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack st = new Stack();
- st.Push('A');
- st.Push('M');
- st.Push('G');
- st.Push('W');
- Console.WriteLine("Current stack: ");
- foreach (char c in st)
- {
- Console.Write(c + " ");
- }
- Console.WriteLine();
- st.Push('V');
- st.Push('H');
- Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
- Console.WriteLine("Current stack: ");
- foreach (char c in st)
- {
- Console.Write(c + " ");
- }
- Console.WriteLine();
- Console.WriteLine("Removing values ");
- st.Pop();
- st.Pop();
- st.Pop();
- Console.WriteLine("Current stack: ");
- foreach (char c in st)
- {
- Console.Write(c + " ");
- }
- }
- }
- }
- Ex: 2
- using System;
- using System.Collections;
- public class SamplesStack {
- public static void Main() {
- // Creates and initializes a new Stack.
- Stack myStack = new Stack();
- myStack.Push( "The" );
- myStack.Push( "quick" );
- myStack.Push( "brown" );
- myStack.Push( "fox" );
- // Displays the Stack.
- Console.Write( "Stack values:" );
- PrintValues( myStack, '\t' );
- // Removes an element from the Stack.
- Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
- // Displays the Stack.
- Console.Write( "Stack values:" );
- PrintValues( myStack, '\t' );
- // Removes another element from the Stack.
- Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
- // Displays the Stack.
- Console.Write( "Stack values:" );
- PrintValues( myStack, '\t' );
- // Views the first element in the Stack but does not remove it.
- Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );
- // Displays the Stack.
- Console.Write( "Stack values:" );
- PrintValues( myStack, '\t' );
- }
- public static void PrintValues( IEnumerable myCollection, char mySeparator ) {
- foreach ( Object obj in myCollection )
- Console.Write( "{0}{1}", mySeparator, obj );
- Console.WriteLine();
- }
- }
- Generic collection
- Explain array to her
- Interfaces they implement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement