Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I love Tuples

I love Tuples

Scheduled Pinned Locked Moved The Lounge
csharp
15 Posts 12 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    GKP1992 wrote:

    Create a nested tuple and now you don't know which element you are referring to when you say Item2

    Tuple "items" can be named, so: var name = GetMyName(); where:

    (string firstName, string lastName) GetMyName()
    {
    return ("Marc", "Clifton");
    }

    I can use name.firstName and name.lastName Most of the time. ;)

    Latest Articles:
    A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #5

    But is that C#?

    H 1 Reply Last reply
    0
    • P PIEBALDconsult

      But is that C#?

      H Offline
      H Offline
      honey the codewitch
      wrote on last edited by
      #6

      Yes

      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

      1 Reply Last reply
      0
      • B Behzad Sedighzadeh

        IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write. It can be compared to Generic lists.

        Behzad

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #7

        I don't use them much. I usually prefer to create a class. It kinda depends on how important the thing is.

        1 Reply Last reply
        0
        • M Marc Clifton

          Yes, tuples are great, especially as a replacement for out string foo and I use them primarily for returning multiple things for rather low level methods when out or a C# class/struct is just overkill. The fact that the tuple parameters can be named was a huge advancement, rather than having to use Item1, Item2, etc. That said, I use them judiciously and always ask myself, if I'm using a tuple here, is that the right approach or am I compensating for a possibly bad "design." For example (this from code I have in a library):

              public (HttpStatusCode status, string content) Get(string url, Dictionary headers = null)
              {
                  var client = RestClientFactory();
                  var request = new RestRequest(url, Method.Get);
          
                  headers?.ForEach(kvp => request.AddHeader(kvp.Key, kvp.Value));
                  RestResponse response = client.Execute(request);
          
                  return (response.StatusCode, response.Content);
              }
          

          Why am I parsing out the status code and content instead of just returning the response object? One answer is that returning response may probably require a using RestSharp and even a reference to the RestSharp package in the caller project. OK, maybe that's a defensible argument, maybe not. After using this library of mine (REST is just one small part of this library) I'm not that thrilled with my initial wrapper implementation. But because I started this "pattern", it continues, like:

              public (T item, HttpStatusCode status, string content) Get(string url, Dictionary headers = null) where T : new()
              {
                  var client = RestClientFactory();
                  var request = new RestRequest(url, Method.Get);
          
                  headers?.ForEach(kvp => request.AddHeader(kvp.Key, kvp.Value));
                  RestResponse response = client.Execute(request);
                  T ret = TryDeserialize(response);
          
                  return (ret, response.StatusCode, response.Content);
              }
          

          And this illustrates mashing together various potentially bad implementation/designs. The tuple now returns three things, and the TryDeserialize catches exceptions silently, returning a null for T item, and what if I want the actual deserialization exception? And now that I look at that code again after a couple years, what's with that AddHeader loop when there's a perfectly

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #8

          :-D I think there are very few people smarter than you.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • B Behzad Sedighzadeh

            IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write. It can be compared to Generic lists.

            Behzad

            T Offline
            T Offline
            TNCaver
            wrote on last edited by
            #9

            I love them because sometimes it is convenient to return more than one value from a method, and the use case doesn't justify the overhead of creating and populating a POCO.

            There are no solutions, only trade-offs.
               - Thomas Sowell

            A day can really slip by when you're deliberately avoiding what you're supposed to do.
               - Calvin (Bill Watterson, Calvin & Hobbes)

            1 Reply Last reply
            0
            • B Behzad Sedighzadeh

              IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write. It can be compared to Generic lists.

              Behzad

              Sander RosselS Offline
              Sander RosselS Offline
              Sander Rossel
              wrote on last edited by
              #10

              Tuples and anonymous classes have drastically reduced the number of single-use classes in my code :D

              Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

              B 1 Reply Last reply
              0
              • M Marc Clifton

                GKP1992 wrote:

                Create a nested tuple and now you don't know which element you are referring to when you say Item2

                Tuple "items" can be named, so: var name = GetMyName(); where:

                (string firstName, string lastName) GetMyName()
                {
                return ("Marc", "Clifton");
                }

                I can use name.firstName and name.lastName Most of the time. ;)

                Latest Articles:
                A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #11

                The slightly weird thing that disturbs me about that is that firstName and lastName behave/look a lot like members of a class/struct yet don't have Pascal Case naming. name.firstName vs. name.FirstName It reminds me of Javascript, and well you know what that can inflict on the soul.

                Regards, Rob Philpott.

                1 Reply Last reply
                0
                • M Marc Clifton

                  GKP1992 wrote:

                  Create a nested tuple and now you don't know which element you are referring to when you say Item2

                  Tuple "items" can be named, so: var name = GetMyName(); where:

                  (string firstName, string lastName) GetMyName()
                  {
                  return ("Marc", "Clifton");
                  }

                  I can use name.firstName and name.lastName Most of the time. ;)

                  Latest Articles:
                  A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

                  R Offline
                  R Offline
                  raddevus
                  wrote on last edited by
                  #12

                  Nice example. I fired up C# Repl and tried it out. :thumbsup:

                  1 Reply Last reply
                  0
                  • B Behzad Sedighzadeh

                    IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write. It can be compared to Generic lists.

                    Behzad

                    J Offline
                    J Offline
                    Jeremy Falcon
                    wrote on last edited by
                    #13

                    I don't use C# much these days but totally agree. And, digging the positivity man. :cool:

                    Jeremy Falcon

                    1 Reply Last reply
                    0
                    • B Behzad Sedighzadeh

                      IMHO, in recent years, the best feature MS has added to the C# is Tuple. It helps make code cleaner and quicker to write. It can be compared to Generic lists.

                      Behzad

                      D Offline
                      D Offline
                      Daniel Pfeffer
                      wrote on last edited by
                      #14

                      Like (almost) everything in C#, they have their uses. Loving them? No!

                      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                      1 Reply Last reply
                      0
                      • Sander RosselS Sander Rossel

                        Tuples and anonymous classes have drastically reduced the number of single-use classes in my code :D

                        Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                        B Offline
                        B Offline
                        Behzad Sedighzadeh
                        wrote on last edited by
                        #15

                        100% agreed!

                        Behzad

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups