C# Collections – Arrays – 1st Part

Hi Friends,

In this section, we are going to start by digging inside Arrays. Arrays one of the most basic yet important piece of C# Collections. One must have profound knowledge of Arrays in order to understand different collection types better. below in the example i have created a simple array for months of year. Any array declaration begins with [] bracket

Now, when i ran the above program in debugging mode, you notice that 1st of all it listed all the elements what i listed there and also in the same order how i have given there. It means its Arrays are ordered list and index based.

1st

Below, in the script, i have added couple of lines to illustrate accessing the element based on index.

2nd

we can write more generalize way of this by giving user flexibility to enter the option like shown below.

Now, when i see the output of this, it will look something like below

3rd

Also, if i have to enumerate through this array, then i will simple write one foreach loop as shown below

4th

Till now, we have seen all readonly operations, now lets see few write operations. Below, snippet will simply go ahead and replace the item in the array. However, replace is equivalent to remove and add element to array, but replace is quite efficient doing the same job.

5th

Now, there are few important points about Arrays like they have their own syntax in C#, you don’t declare any other type using [] like we do with generics. Also, their strong typing is implemented at CLR level itself. However, many collections like List under the hood using Arrays for their implementation. So, List is basically dependent on T[].

One more point to note here is Arrays are reference types. so, the very basic test to check this by initializing some value and see the same in debugger, for value types it will be default value for that variable and for reference type it will be null as shown below in the screen shot.

6th

7th

However, since Arrays are reference types, so we can go ahead and create the Array with New keyword as well as shown below in the example.

Now, since i have initialized my array here with int type, so now when i go ahead and the see the same then, it will present me the default values as shown below in the screen shot.

8th

However, in the below example, I have created and initialized the array as well.

Now, when i see the same in debugger, it will present the below values.

9th

One point to note here you can’t initialize array in Array constructor, .Net don’t support that. Only thing which you can do with array constructors is allocate the memory for that.However, C# compiler is very flexible to understand the intention of coder as explained below. here, i have done the array initialization but different ways, all are producing the same result.

next look at enumerating the array, we have already seen implicit style of enumeration using foreach loop, however we can also step through explicitly and the easiest way is using for loop. below i have shown one sample snippet for the same.

One more point to note here is that foreach is readonly operation. means you can’t set values to variables inside foreach loop. With this i would like to conclude the 1st section of the Array. In the next section, we’ll do deep dive under the hood of array.

Thanks,
Rahul
Happy Coding

Posted in C# | Tagged

C# Collections – 1st Part

Hi Friends,

Today in this section, i’m going to talk about variety of C# collections. Here, in this course i’m going to cover below listed collections.

    • Introduction to C# Collections
    • Arrays
    • Array Types
    • Interfaces
    • Lists
    • Linked Lists, Stacks, Queues
    • Dictionaries
    • Sets
    • Enumerators
    • Multi Dimensional Arrays, Ranks and Bound

so, before we jump in and starts learning collection. we should stop a moment and think and think what a collection is. In, .Net Terminology “Collection means a type responsible for managing objects in memory and to provide access to those objects”. so, without wasting let’s gets started. we’ll 1st begin with Various collection definitions and then in the 2nd part we’ll start with the implementation.

  1. Lists:- In many scenarios order of the collection is very important, like consider a example of most popular movies of the year. so, in order to present this set we need to use that collection, which can present this set in ordered fashion, hence List is the ideal choice for that. Apart from that it also gives flexibility to access the element based on its index starting with index 0 and so on. Hence, this is also known as zero based indexing. Now, below I have mentioned List types in .Net     T[], List<T> , Collection<T>, ReadonlyCollection<T>, ObservableCollection<T>, IList<T>. Also one point to note here that index based collections are very efficient than other collections.
  2. Dictionaries:- A dictionary is a very different type of collection than list. Here, it’s not going to be referred by its index rather by name itself. so, name here actually means Key and key is nothing but the unique item in the dictionary and on that basis only it is going to be extracted from the dictionary. so, most widely used dictionary is of this signature Dictionary<TKey, TValue>. Now, the standard contract for Dictionary is defined by IDictionary<TKey, TValue>. Dictionaries are also pretty fast but Lists are having edge over dictionaries.
  3. Sets:- Sets are little different in concept from Lists and Dictionaries. With sets, we are not going to access individual element rather treating the whole collection as a single group. here, scenario could be to compare two sets and find the common elements present in both the sets. so, in that case we apply intersection of sets. similarly, for different scenarios if asked to apply union of sets, then we can also apply on these sets. Now, in .Net most common set collection is called HashSet<T>. Now, there is also an interface which defines standard contract for set collection and that is know as ISet<T>. One more thing to note here that there is similarity between Dictionaries and Sets as the underlying technology powering both is hashtable. However, sets don’t have a key.

Now, this is also important to understand that how collection operations work. 1st operation is reading from the collection and 2nd is writing to the collection. so, reading is quite simple like you might want to lookup a single element or might want to enumerate in the collection. So, for look ups we either provide index or key and the collection provides the requested items. Enumerating a collection is also very easy job in C#. It normally gets achieved by using foreach() loop. Now, when it comes to modify an item, there are two fundamental operations, like Add an Item or Remove an Item. these are only 2 meaningful operations for Dictionaries and Sets. However, for Lists, you can add item at a particular place as well.

In the 2nd module, we’ll begin delving each of these collections and their usages in details. Till then stay tuned and Happy coding.

Thanks,
Rahul

Posted in C# | Tagged