65.9K
CodeProject is changing. Read more.
Home

Enabling C#8 'Switch Features in VS 2017, NET 7.2

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (5 votes)

Jul 6, 2018

CPOL

1 min read

viewsIcon

8742

how to get them installed, available

Introduction

It's a bit tricky, since on-line non-MS code examples are incomplete.

Using the Code

  1. Follow the directions here to install the Roslyn_CSharp_8_Preview:
    https://github.com/dotnet/csharplang/wiki/vNext-Preview#installation-instructions
  2. Copy the code here into your project:
    https://raw.githubusercontent.com/dotnet/csharplang/master/proposals/ranges.cs
  3. Using NPM in Visual Studio, install https://www.nuget.org/packages/System.Memory/

Step #3 will get you the Span<T> feature needed for using the ranges.cs code.

Note: Future of Span: Richard Landry of MS: "Span is included in .NET Core 2.1. We explored including Span in .NET Framework 4.8 and decided against it due to compatibility concerns for existing applications. You can get access to Span and additional related types in the System.Memory Nuget package which enables some of the scenarios that are enabled on .NET Core."

An Example

Note: Don't be surprised if your code gets "lit up" with the kind of red squiggles usually seen when symbols can't be resolved by the compiler, or you get (with ReSharper) "unreachable code" pop-up messages. That's the price you pay for exploring new worlds. :)

My 2-cents worth: feels weird to use 'when in what seems to me the same way we use 'where in Linq.

I also find having to repeat "var p when ,..." tedious, but, keep in mind that makes sense when each "case" statement" is going to declare an internal variable of a different Type. See the 'Person class example in Jonathan Allen's article here for that.

Use it like this:

// example of use in a method or whatever:

    Passenger p1 = new Passenger("joe", 32);   
       
    private void test()
    {
       var fare = p1.GetPaxAge();
    }

// code

    public class Passenger
    {
        public Passenger(st4ing name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; }
        public int Age { get; }
    }

    public enum PaxAge
    {
        Infant, Youth, Teen, Adult, Senior
    }

    public static class PaxExtension
    {
        public static PaxAge GetPaxAge(this Passenger pax)
        {
            int age = pax.Age;

            return pax switch
            {
                var p when adult.Contains(age) => PaxAge.Adult,
                var p when teen.Contains(age) => PaxAge.Teen,
                var p when youth.Contains(age) => PaxAge.Youth,
                var p when infant.Contains(age) => PaxAge.Infant,
                _ => PaxAge.Senior
            };
        }

        static List<int> infant = 0.GetRange(2);
        static List<int> youth = 3.GetRange(12);
        static List<int> teen = 13.GetRange(18);
        static List<int> adult = 19.GetRange(59);
    }

    public static class IEnumExtensions
    {
        public static List<int> GetRange(this int start, int end)
        {
           return Enumerable.Range(start, end - start + 1).ToList();
        }
    }