Skip to main content

Posts

Showing posts from October, 2011

c# - How to convert a Stack to an Array

Convert a Stack to an Array The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we convert a Stack instance to an Array instance. That means we will copy a Stack to a new Array. In this .net c# tutorial code, we used the Stack class ToArray() method to create an Array instance from a Stack object. The Stack class ToArray() method copy the Stack to a new array. This method returns a new array containing copies of the elements of the Stack. The elements are copied onto the array in last-in-first-out order. That is similar to the order of the elements returned by a succession of calls to Pop. StackToArrayMethod.aspx <%@ Page Lang...

c# - How to insert an object at the top of the Stack

Insert an object at the top of the Stack The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we insert an object at the top of the Stack. In this .net c# tutorial code, we used the Stack class Push() method to insert an object at the top of a Stack. The Stack class Push(Object) method inserts an object at the top of the Stack. The Stack class Push(object? obj) method has a parameter named obj. The obj parameter is the Object to push onto the Stack. The value can be null. StackPushMethod.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html> <script runat="server"> ...

c# - Remove and get the object at the top of the stack

Remove and get the object at the top of the Stack The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can remove and get the object at the top of the Stack instance. That means we will remove the object and get the object at the top of the Stack at the same time. In this .net c# tutorial code, we used the Stack class Pop() method to remove and get the object at the top of a Stack. The Stack class Pop() method removes and returns the object at the top of the Stack. The Pop() method returns the Object removed from the top of the Stack. The Stack class Pop() method throws InvalidOperationException if the Stack is empty. The Pop() ...

c# - How to get the object at the top of the stack

Get the object at the top of the Stack The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can get the object at the top of the Stack instance. That means we will only get the object at the top of the Stack without removing it from the Stack. In this .net c# tutorial code, we used the Stack class Peek() method to get the object at the top of a Stack without removing/deleting the object from the Stack. The Stack class Peek() method returns the object at the top of the Stack without removing it. The Stack Peek() method returns the Object at the top of the Stack. This method throws InvalidOperationException if the Stack is empty. ...

c# - How to copy a Stack to an Array

Copy a Stack to an Array The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can copy a Stack to an Array. That means we will copy a Stack instance to an Array, starting at the specified starting index of the Array instance. In this .net c# tutorial code, we used the Stack class CopyTo() method to copy Stack items to an Array object. The Stack class CopyTo(Array, Int32) method copies the Stack to an existing one-dimensional Array, starting at the specified array index. The Stack class CopyTo(Array array, int index) method has two parameters. The array parameter is the one-dimensional Array that is the destination of the elements ...

c# - How to count Stack elements

Count Stack elements The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can count Stack elements. In this .net c# tutorial code, we used the Stack class Count property to count Stack items. The Stack class Count property gets the number of elements contained in the Stack. The Stack Count property value is an Int32 which is the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store but the Count is the number of elements that are actually in the Stack. StackCountProperty.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html> <sc...

c# - How to check whether an element is in the stack

Determine whether an item is in the Stack The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can check whether an item is in the Stack or not. In this .net c# tutorial code, we used the Stack class Contains() method to determine whether an element exists in the Stack instance. The Stack class Contains(Object) method allows us to determine whether an element is in the Stack. The Stack class Contains(object? obj) method has a parameter named obj. The obj parameter is the object to locate in the Stack. The value can be null. The Stack class Contains(Object) method returns a Boolean value. The method returns true if the obj is f...

c# - How to remove all objects from the stack

Remove all objects from the Stack The Stack class represents a simple last-in-first-out non-generic collection of objects. The Stack capacity is the number of elements it can hold. When elements are added to a Stack, its capacity is automatically increased as required through reallocation. The Stack accepts null as a valid value and allows duplicate elements. The following .net c# tutorial code demonstrates how we can remove all objects from the Stack. That means we will delete all elements from a Stack instance. In this .net c# tutorial code, we used the Stack class Clear() method to remove all items from a Stack instance. The Stack class Clear() method removes all objects from the Stack. This method set the Count is zero, and references to other objects from elements of the collection are also released. StackClearMethod.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html> <script runat="server...

c# - How to make an ArrayList read-only

Make an ArrayList read-only The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can make an ArrayList read-only. That means we will convert an ArrayList instance to a read-only ArrayList. We also check whether an ArrayList instance is read-only or not. Here we will use the ArrayList class ReadOnly() method to make an ArrayList read-only. We also use the ArrayList class IsReadOnly property to determine whether an ArrayList is read-only or not. A collection that...

c# - How to copy elements over a range in ArrayList

Copy the elements of a collection over a range of elements in an ArrayList The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can copy the elements of a collection over a range of elements in an ArrayList instance. Here we will use the ArrayList class SetRange() method to copy elements over a range of elements in the ArrayList object. The ArrayList SetRange(Int32, ICollection) method copies the elements of a collection over a range of elements in the Array...

c# - How to reverse ArrayList range of elements order

Reverse order of a range of elements in an ArrayList The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can reverse the order of a range of elements in an ArrayList instance. That means we will reverse the order of the elements in a specified range within an ArrayList object. Here we will use the ArrayList class Reverse() method to reverse a range of element’s order. The ArrayList Reverse() method reverses the order of the elements in the ArrayList or a port...

c# - How to remove a range of elements from ArrayList

Remove a range of elements from an ArrayList The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can remove a range of elements from an ArrayList instance at once. That means we will delete multiple elements from an ArrayList object. Here we will use the ArrayList class RemoveRange() method to remove a range of elements from an ArrayList instance. The ArrayList RemoveRange(Int32, Int32) method removes a range of elements from the ArrayList. The RemoveRange(in...

c# - How to insert multiple elements into an ArrayList

Insert multiple elements into an ArrayList at once The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can insert multiple elements into an ArrayList at once. That means we add a collection of elements into an ArrayList at the specified index position. Here we will use the ArrayList class InsertRange() method to add multiple elements to an ArrayList instance. The ArrayList InsertRange(Int32, ICollection) method inserts the elements of a collection into the Ar...

c# - How to get the index of an element within a range in ArrayList

Get the index of an element within a range of elements in an ArrayList The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can get the index of an element within a range of elements in an ArrayList. That means we will find the specified element within a range of elements in an ArrayList. Here we will use the ArrayList class IndexOf() method to get the index of an element within a range in an ArrayList object. We only take the index of the element’s first occurren...

c# - How to get an element index in ArrayList by the starting index

Get an element index from ArrayList by specified starting index The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can get the index of an element in an ArrayList instance by using the starting index. That means we will find the specified element within ArrayList from the specified index position. Here we will use the ArrayList class IndexOf() method to get the index of an element in an ArrayList object. We only take the index of the element’s first occurrence wit...

c# - How to get the index of an ArrayList element

Get the index of an element in ArrayList The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can get the index of an element in an ArrayList instance. Here we will use the ArrayList class IndexOf() method to get the index of an element in an ArrayList object. We only take the index of the element’s first occurrence within an ArrayList. The ArrayList IndexOf() method returns the zero-based index of the first occurrence of a value in the ArrayList or in a porti...

c# - How to copy a range of ArrayList elements to an Array

Copy a range of elements from the ArrayList to an Array The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements. The following .net c# tutorial code demonstrates how we can copy an ArrayList specified elements (a range of elements) to an Array. Here we used the ArrayList class CopyTo() method to copy an ArrayList to an Array instance. The ArrayList CopyTo() method copies the ArrayList or a portion of it to a one-dimensional array. The ArrayList CopyTo(Int32, Array, Int32, Int32) method...

c# - How to convert Dictionary keys into array

How to convert Dictionary Keys into array .Net framework Dictionary<TKey, TValue> class represents a collection of keys and values. the Dictionary<TKey, TValue>.Keysproperty allow us to get a collection containing the keys in the Dictionary<TKey, TValue>. this Keys property value type isSystem.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection. to convert a dictionary keys to array, first we need to get the dictionary keys by its 'Keys' property. this property return acollection (generic list) of dictionary keys. after getting the List<T>, we can convert it to an array by List class ToArray() method. List<T>.ToArray() method allow us to copy the elements of the List<T> to a new array. so we can convert the dictionary keys into array asDictionary<TKey, TValue>.Keys.ToArray(). the following asp.net c# example code demonstrate us how can we convert dictionary keys to an array ...

c# - How to get a range of elements from a list

Generic List GetRange() Method .Net framework generic list GetRange() method allow us to create a shallow copy of a range of elements in the source List<T>.the GetRange() method is exists under System.Collections.Generic namespace. this method require to pass two parameters, named 'index' and 'count'. both 'index' and 'count' parameters data type is System.Int32. the 'index' represent the zero based List<T> index at which the range startsand the 'count' represent the number of elements in the range. this method return value type is System.Collections.Generic.List<T>. this returned List<T> is a shallow copy of a range of elements in the sourceList<T>. this method throw ArgumentOutOfRangeException exception if the 'index' is less than zero (0) or 'count' is less than 0. this method also throwArgumentException exception if the 'index' and 'count...