0

I have the below Extension (not mine, borrowed for my implementation, but altered a bit) and I am wondering why is the TryGetValue below failing, despite the fact the the pair (string, object - well, var in my case) seem correct.

public static class somExt
{
    private static string TimeoutPropertyKey = "RequestTimeout";

    public static void SetTimeout( this HttpRequestMessage request, TimeSpan? timeout)
    {
        if (request == null)
            throw new ArgumentNullException(nameof(request));

        request.Properties[TimeoutPropertyKey] = timeout;
    }

    public static TimeSpan? GetTimeout(this HttpRequestMessage request)
    {
        if (request == null)
            throw new ArgumentNullException(nameof(request));

        if (request.Properties.TryGetValue(
                TimeoutPropertyKey, out var value)  // error CS1003: Syntax error, ',' expected - why??
            && value is TimeSpan timeout)
            return timeout;
        return null;
    }
}

Anyone could help me understand and correct the error? Thank you

Nick
  • 483
  • 1
  • 6
  • 15
  • 2
    An obvious question. What is an error? – Optional Option Jan 31 '20 at 20:12
  • I didn't see any errors, the code works just fine. @Nick How did you set the value actually? – Pavel Anikhouski Jan 31 '20 at 20:17
  • Apologies, I edit the error received in comment in code above. – Nick Jan 31 '20 at 20:18
  • 1
    I don't get that compilation error with the posted code. Maybe your code file contains some funky characters? – juharr Jan 31 '20 at 20:21
  • @juharr Sorry, can't do, will introduce error, asks to create property or variable for the timeout..(no funky characters, I just copied and pasted and is on top level, means, right under my namespace declaration .. i am equally confused..) Thank you very much for looking into that.. – Nick Jan 31 '20 at 20:21
  • 6
    What version of C# are you using? `out var` was not legal prior to C# 7. – D Stanley Jan 31 '20 at 20:24
  • Does this answer your question? [Unknown CS1003 Error](https://stackoverflow.com/questions/41373260/unknown-cs1003-error) – Lance U. Matthews Jan 31 '20 at 20:27
  • Or this: [VS 2017 - New C# 7 feature for out parameters causes project not to build](https://developercommunity.visualstudio.com/content/problem/38971/vs-2017-new-c-7-feature-for-out-parameters-causes.html) – Jimi Jan 31 '20 at 20:31
  • @D Stanley - VS 2017 and C# latest major (default) whatever that means (I tried to set it to C# 7.3 but I got error: Severity Code Description Project File Line Source Suppression State Error CS1617 Invalid option '7.3' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. – Nick Jan 31 '20 at 20:40
  • @Nick What kind of project is this? Does installing/updating the [`Microsoft.Net.Compilers` nuget package](https://www.nuget.org/packages/Microsoft.Net.Compilers/), as described [here](https://stackoverflow.com/q/42216740/150605) and [here](https://www.c-sharpcorner.com/article/enabling-c-sharp-7-compilation-with-visual-studio-2017/), solve the problem? – Lance U. Matthews Jan 31 '20 at 21:34
  • @BACON thank you for looking it up, drives me crazy, problem didn't solve... – Nick Feb 03 '20 at 09:26
  • Could someone help me debug this thing? Tried to declared `out var value` a line before, as object value;, and didn't work, as all other attempts of mine.. – Nick Feb 03 '20 at 09:32
  • The quick fix is `TimeSpan value; if (request.Properties.TryGetValue(TimeoutPropertyKey, out value) { }`, which would work on any version of the C# compiler. (By the way, whether you use that or the code in the question with `out var value` passed as a parameter, the `&& value is TimeSpan timeout` is unnecessary because `value` can only ever be a `TimeSpan`, and that is known at compile time.) The real issue is why your compiler only supports up to C# 6.0 when [Visual Studio 2017 shipped with 7.0](https://docs.microsoft.com/dotnet/csharp/language-reference/configure-language-version). – Lance U. Matthews Feb 03 '20 at 19:33

0 Answers0