Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
5 Jul 2018CPOL1 min read 8.6K   3  
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:

C#
// 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();
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Thailand Thailand
Human being, mortal, flawed.

Comments and Discussions

 
-- There are no messages in this forum --