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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events