Skip to content

Add a factory method for KeyValuePair utilizing type inference #4779

Closed

Description

@kbilsted

The Tuple.Create() is a generic factory method that works by type inference making it much slicker to create tuples than using the Tuple<,>-constructor.

I propose a similar implementation pattern for the KeyValuePair<,> type. Hence something a long the lines of

public static class KeyValuePair
{
    public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
    {
         return new KeyValuePair<TKey, TValue>(key, value);
    }
}

Activity

  1. RedwoodForest commented on Dec 11, 2015

    @RedwoodForest

    dotnet/roslyn#35 would add type inference to constructors so you wouldn't need the factory method.

  2. transferred this issue fromdotnet/coreclron Jan 30, 2020
  3. added this to the Future milestone on Jan 30, 2020
  4. added
    untriaged
    on Feb 26, 2020
  5. added
    api-needs-work
    and removed
    untriaged
    on Jul 1, 2020
  6. joperezr commented on Jul 1, 2020

    @joperezr
    Member

    In order to consider this we need an actual Api Proposal so we can move it forward.

  7. kbilsted commented on Jul 6, 2020

    @kbilsted
    Author

    @joperezr thanks for getting back.

    What is it you need more than the above explanation? Do I need to fill out a template? The code above is the whole code to include

  8. joperezr commented on Jul 6, 2020

    @joperezr
    Member

    first of all sorry it took so long to look at this, it got missed somehow. Usually ApiProposals have the following template: https://github.com/dotnet/runtime/blob/master/.github/ISSUE_TEMPLATE/02_api_proposal.md.

    Here is a good set of steps we have with good examples of how a good API proposal looks like as well as what is the general process of it: https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md#steps

    Let me know if you have any questions around it.

  9. GrabYourPitchforks commented on Apr 15, 2021

    @GrabYourPitchforks
    Member

    Is this obviated by the C# target typed new feature?

    using System.Collections.Generic;
    
    public class C {
        public static KeyValuePair<string, int> M() {
            return new("hello", 42);
        }
    }
  10. ghost locked as resolved and limited conversation to collaborators on May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Runtimeenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions