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. A Blatant Programming Question

A Blatant Programming Question

Scheduled Pinned Locked Moved The Lounge
questioncsharpdata-structureshelptutorial
60 Posts 28 Posters 0 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 mgkr

    "save you from creating a throwaway class" ^^This Ask yourself how many times you've (mis-)used the out param, to have method "return" two values. Sometimes you need two values back, and creating a class/struct for just that one method to be able to return the two values is.. annoying (and clutters the code) Using a Tuple instead is the "clean" way of doing this (and it can be expanded to more than just two objects)

    D Offline
    D Offline
    DavidSherwood
    wrote on last edited by
    #51

    The 'clean' way is not so clean because C# (and VB) do not have any language constructs to handle them. A function that would return a tuple will require some 'dirty' code to untangle the tuple. I thing tuples were added to .Net to support F#, which does make language constructs to deal with them. Until C# adds similar constructs, I don't see using them.

    M 1 Reply Last reply
    0
    • R Rob Grainger

      Not sure if you actually got an answer here, but the 20 or so results I scanned seemed hopelessly misguided. Tuples are used enormously in functional programming and are extremely useful in LINQ. With LINQ queries, you may have an object of type person with Name, Age, Gender, etc. Now if you execute "from Persons where age > 20 select Age, Gender", what is the type of the result: answer a tuple type with the first item an integer and the 2nd an enum type. This is where they are useful. They avoid having to litter your code with classes that add no meaningful information and are pure data values. They are commonly useful as return types, e.g. a tuple where the first item indicates if the 2nd is applicable. Many more usages exist.

      R Offline
      R Offline
      Roger Wright
      wrote on last edited by
      #52

      Cool! That makes sense to me! :-D

      Will Rogers never met me.

      R 1 Reply Last reply
      0
      • U User 2788383

        One of the best things tuples do is they allow you to return multiple values from a function instead of just one value as was the case in C# before C# 4.0.

        R Offline
        R Offline
        Roger Wright
        wrote on last edited by
        #53

        I haven't needed to do that often, but I can see the value of this for a number of applications. Thanks! :-D

        Will Rogers never met me.

        1 Reply Last reply
        0
        • K Kenneth Haugland

          :laugh: Only the items could be anything, seems very much to resemble a list of different objects, witch you could send around without createing a class. I cant think of a use. WPF and binding seem to make them unnessecary, but what do I know... Im sure that someone will tell you that the planets existens depended on this class...

          M Offline
          M Offline
          Member 4608898
          wrote on last edited by
          #54

          Probably a direct mapping to the javascript array which can take just about anything.

          1 Reply Last reply
          0
          • R Roger Wright

            Cool! That makes sense to me! :-D

            Will Rogers never met me.

            R Offline
            R Offline
            Rob Grainger
            wrote on last edited by
            #55

            Glad it was helpful.

            1 Reply Last reply
            0
            • R Roger Wright

              Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)

              Will Rogers never met me.

              S Offline
              S Offline
              S Douglas
              wrote on last edited by
              #56

              Hi Roger, Just happened upon this thread (been a few days since I was last at CP on this computer). Turples are used heavily in MDX (Multi dimensional Expressions, for Multi Dimensional databases such as SSAS) where they create a subset of data on the fly. The data is not entirely known (the multi dimensional database does know the meta data of the database) until the user selects the data. I would guess that the class was added to aid in building MDX query interfaces.


              Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

              1 Reply Last reply
              0
              • D DavidSherwood

                The 'clean' way is not so clean because C# (and VB) do not have any language constructs to handle them. A function that would return a tuple will require some 'dirty' code to untangle the tuple. I thing tuples were added to .Net to support F#, which does make language constructs to deal with them. Until C# adds similar constructs, I don't see using them.

                M Offline
                M Offline
                mgkr
                wrote on last edited by
                #57

                The "clean" was in ""'s - As it was compared to the misuse of the out param. Strictly speaking I agree it's not clean, and one ought to create a new class/struct. But... I also don't like to clutter my code with tiny helper classes with no other functionality than storing a few values, so I can return it from a method call. Pest or cholera... But either way, I still think it's "cleaner" (can we agree on that :cool:) than misusing the out parameter for this purpose (something I've also done... :-O )

                1 Reply Last reply
                0
                • R Roger Wright

                  Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)

                  Will Rogers never met me.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #58

                  There are a lot of uses for tuples in math, e. g. in group theory. They have been used in these areas for much longer than computers exist. Most of the time, the elements of a tuple in math are of a radically different kind. E. g. when you define an algebraic group, you build a tuple out of the basic set of numbers that your elements are based on (e. g. real numbers, complex numbers, integers, or something more exotic such as functions), and the rules for performing operations on your elements, such as addition, multiplication, whether these operations are associative or commutative, etc.. While I can imagine some mathematician rejoices at the ability to model such constructs in software directly, IMHO a simple class would serve the same purpose. Only better.

                  R 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    There are a lot of uses for tuples in math, e. g. in group theory. They have been used in these areas for much longer than computers exist. Most of the time, the elements of a tuple in math are of a radically different kind. E. g. when you define an algebraic group, you build a tuple out of the basic set of numbers that your elements are based on (e. g. real numbers, complex numbers, integers, or something more exotic such as functions), and the rules for performing operations on your elements, such as addition, multiplication, whether these operations are associative or commutative, etc.. While I can imagine some mathematician rejoices at the ability to model such constructs in software directly, IMHO a simple class would serve the same purpose. Only better.

                    R Offline
                    R Offline
                    Roger Wright
                    wrote on last edited by
                    #59

                    Now that you mention it, I've probably used tuples many times. After all, a state variable matrix of a control system often contains very different quantities in a state vector. That is a sort of tuple, even though the actual values are nearly always real or complex numbers.

                    Will Rogers never met me.

                    S 1 Reply Last reply
                    0
                    • R Roger Wright

                      Now that you mention it, I've probably used tuples many times. After all, a state variable matrix of a control system often contains very different quantities in a state vector. That is a sort of tuple, even though the actual values are nearly always real or complex numbers.

                      Will Rogers never met me.

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #60

                      Actually, in the meantime I did conceive one reasonable use-case for tuples: functions that return multiple values. E. g. when you cut a curve with a surface, you may be interested not only in the resulting 3d-point, but also the parameters used to generate that point on the curve and the surface (assuming they are defined as parametric functions). There may also be no or multiple cut points, and various border cases that you may want to indicate in some way.

                      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