C# Corner

What's New with .NET Framework 4.6, Part 1: API Updates

For this first in a series, Eric Vogel checks out the new IReadOnlyCollection<T> Stack<T> and Queue<T> implementations in .NET Framework 4.6.

With the release of Visual Studio 2015 came with, of course, a new version 4.6 of the .NET Framework 4.6. Just within .NET 4.6 were a multitude of changes, including API updates, C# 6.0, VB 14 and a new set of optimized 64-bit compilers. Many of the advantages in .NET Framework 4.6 can be had just by recompiling your application in the newer framework.

We'll cover all of it over the next few months, but this time out, I'll be covering some of the new APIs that are available in .NET 4.6. In particular, we'll look at the IReadOnlyCollection<T> generic interface that now is implemented by Stack<T> and Queue<T>.

Before I go into details let's go over the basics of stacks and queues. A stack is a collection type where the items are retrieved in a last-out/first-in order. You can think of the collection like a stack of papers. You always get the last added item when you retrieve an item. A queue is a collection where items are retrieved first in first out order. You can think of a queue like waiting in line for a movie. The first person in line is also the first person out of the line.

The IReadOnlyCollection<T> generic interface is pretty basic: It implements the IEnumerable<T> interface and counts a Count property that is the number of items in the collection. The interface IReadOnlyCollection<T> would be well suited for interfaces where you only want to expose a read-only version of the underlying collection.

First let's look at how to use the new Stack<T> implementation of IReadOnlyCollection<T>. A stack is a basic collection type where the items are retrieved in last-in/first-out order. First I instantiate a new stack and push on the numbers 1, 2, and 3:

var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);

Next I assign an IReadOnlyCollection<int> variable to the stack and output the items and then a count:

IReadOnlyCollection<int> readOnlyColl = s
 Console.WriteLine("Stack contains:");
 foreach (var item in readOnlyColl)
 {
     Console.WriteLine(item);
 }

Console.WriteLine(string.Format("Stack has {0} items", readOnlyColl.Count)); As you would expect the items are retrieved starting at 3 and ending at 1, as shown in Figure 1.

Stack Output
[Click on image for larger view.] Figure 1: Stack Output

Next let's look at the Queue<T> implementation of IReadOnlyCollection<T>. First I create a new queue<int> and add the items 1, 2, and 3:

var queue = new Queue<int>();
 queue.Enqueue(1);
 queue.Enqueue(2);
 queue.Enqueue(3);

Next I assign the same readOnlyColl variable to the queue and output the items in the collection along with the item count:

readOnlyColl = queue;
 Console.WriteLine("Queue contains:");
 foreach (var item in readOnlyColl)
 {
     Console.WriteLine(item);
 }
 Console.WriteLine(string.Format("Queue has {0} items", readOnlyColl.Count));

As you can see in Figure 2, the items are output in first-in/first-out order as expected.

Queue Output
[Click on image for larger view.] Figure 2: Queue Output

See Listing 1 for the completed example source code.

Listing 1: Program.cs

using System;
using System.Collections.Generic;

namespace VSMNet46Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var stack = new Stack<int>();
            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            IReadOnlyCollection<int> readOnlyColl = stack;
            Console.WriteLine("Stack contains:");
            foreach (var item in readOnlyColl)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(string.Format("Stack has {0} items", readOnlyColl.Count));
            Console.ReadLine();
            var queue = new Queue<int>();
            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            readOnlyColl = queue;
            Console.WriteLine("Queue contains:");
            foreach (var item in readOnlyColl)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(string.Format("Queue has {0} items", readOnlyColl.Count));
            Console.Read();
        }
    }
}

As you can see the new IReacOnlyCollection<T> implementations for Stack<T> and Queue<T> are quite useful. I recommend using IReadOnlyCollection<T> when you have an interface that should only have a read-only version of the underlying collection.

Next time, we'll take a peek at some new encryption features that are in .NET Framework 4.6. Stay tuned.

About the Author

Eric Vogel is President and Lead Software Architect at Avant-Garde Labs, LLC. In DeWitt, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube