Bunkerbuster
Bunkerbuster

Reputation: 1001

C# Extensions working in console application but not in MVC web application. Am I missing something?

I used the extension ToRanges and Stringify from (https://codereview.stackexchange.com/a/187442/220021) to experiment. When I use these in a Console application they work as expected.

But when I added them to a MVC application (same .net 4.7.2 framework), I now can not even build (compile errors) the solution. But I do not get any errors in the error list.

..... --> complete path ( i removed this)

1>------ Build started: Project: App, Configuration: Debug Any CPU ------
1>....\StringExtensions.cs(16,35,16,36): error CS1031: Type expected
1>....\StringExtensions.cs(16,35,16,36): error CS1003: Syntax error, '>' expected
1>....\StringExtensions.cs(16,35,16,36): error CS1519: Invalid token '(' in class, struct, or interface member declaration
1>....\StringExtensions.cs(16,45,16,48): error CS1001: Identifier expected
1>....\StringExtensions.cs(16,45,16,48): error CS1002: ; expected
1>....\StringExtensions.cs(16,52,16,53): error CS1003: Syntax error, ',' expected
1>....\StringExtensions.cs(16,55,16,63): error CS1002: ; expected
1>....\StringExtensions.cs(16,55,16,63): error CS1520: Method must have a return type
1>....\StringExtensions.cs(18,19,18,22): error CS1003: Syntax error, '(' expected
1>....\StringExtensions.cs(18,49,18,50): error CS1026: ) expected
etc etc

This is how added the code

using System.Collections.Generic;
using System.Linq;

namespace App.App_Code_classes
{
    public static class StringExtensions
    {

    //source https://codereview.stackexchange.com/a/187442
    public static IEnumerable<(int Min, int Max)> ToRanges(this IEnumerable<int> source)
    {
        using (var e = source.GetEnumerator())
        {
            if (!e.MoveNext())
            {
                yield break;
            }

            var previous = e.Current;

            var range = (Min: previous, Max: e.Current);

            while (e.MoveNext())
            {
                var isConsecutive = e.Current - previous == 1;
                if (isConsecutive)
                {
                    range = (range.Min, e.Current);
                }
                else
                {
                    yield return range;
                    range = (e.Current, e.Current);
                }
                previous = e.Current;
            }

            yield return range;
        }
    }

    //source https://codereview.stackexchange.com/a/187442
    public static string Stringify(this IEnumerable<(int Min, int Max)> ranges)
    {
        var values = ranges.Select(range => $"{(IsEmpty(range) ? $"{range.Min}" : $"{range.Min}-{range.Max}")}");
        return string.Join(", ", values);

              bool IsEmpty((int Min, int Max) range)
             {
                 return range.Max - range.Min == 0;
             }
         }
     }
 }

I am using other extensions in the MVC application and they work as expected, am I missing something? Or how can I tackle this?

Upvotes: 0

Views: 166

Answers (1)

Alex - Tin Le
Alex - Tin Le

Reputation: 2000

It's very likely that your MVC project still using lower C# language version. Because your tuple is only supported in C# 7 (https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#tuples)

Note that this is C# language version, not .NET Framework version.

To check what version, if you're using Visual Studio, you can right click on your project -> Properties -> Build -> Advanced This should list available versions as well as the one your proj is using.

Upvotes: 2

Related Questions