6

If using .Net < 4.7 tuples are not supported. If I install the NuGet "System.ValueTuple", I'm able to write code like

    public static (string Name, int Age) GetFoo()
    {
        return ("Bar", 99);
    }

with .Net 4.6.2. How is that possible? How can a NuGet (DLL) change my available language features? Is there any documentation available about what is going on here? I wasn't able to find any.

Sven Bardos
  • 872
  • 10
  • 27
  • 3
    It doesn't change your language features. The version of the compiler you are using already supports tuples, but it knows that System.ValueTuple may be missing from certain framework versions, so it prompts you to reference the package it is detects a framework version where the struct is missing. – Sean Mar 03 '20 at 14:51
  • I just have discovered that this question has been closed. Seriously? Look at the accepted answer, it describes exactly what's going on. If a precise answer is possible, how can the question be too broad? – Sven Bardos Jul 18 '20 at 21:07

1 Answers1

6

The compiler simply needs certain features to exist to bind to them. In the case of (string Name, int Age) it is looking for System.ValueTuple<T1,T2> - and as long as it finds that, it is happy. It doesn't usually matter from where - if it comes from the current runtime system library: fine; if it comes from a nuget package: also fine!

This has a lot of pedigree; this same approach applies to a lot of things:

  • "LINQ" - just needs the right methods to resolve, whether by extension methods or not (this has allowed "LinqBridge", for example, to retro-fit LINQ onto older runtimes)
  • async/await - just needs the GetAwaiter() etc methods to exist, whether by extension methods or not
  • async iterator blocks and async foreach - just needs the IAsyncEnumerable<T> etc APIs to exist (and the correct builder APIs for async iterators)
  • async dispose - just needs the IAsyncDisposable interface to exist
  • etc

so .NET and C# have enjoyed a long history of allowing nuget packages (or other library updates) to "light up" C# features that are designed for "current" frameworks.

Without those methods / types / etc being available, the compiler still knows about the feature - it just can't use it unless it knows how to attach. If the type / method resolves: you're golden.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900