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

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube