Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 988149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:35:56+00:00 2026-05-16T05:35:56+00:00

I am making an instructional video for C# 4.0 for beginning programmers . For

  • 0

I am making an instructional video for C# 4.0 for beginning programmers.

For every topic I introduce I include a practical example which the student could actually use, for instance, for the improved COM Interop functionality, I show how to create an Excel file and fill it with values from code.

For named and option parameters I show how you can create a logging method with 5 parameters but don’t have to pass any if you don’t want since they all have default values. So they see how calling methods is easier with this feature.

I would also like to introduce tuples as well if I can, but it seems that all the “practical examples” (as in this question: Practical example where Tuple can be used in .Net 4.0?) are very advanced. The learners that use the video learn OOP, LINQ, using generics, etc. but e.g. functional programming or “solving Problem 11 of Project Euler” are beyond the scope of this video.

Can anyone think of an example where tuples would actually be useful to a beginning programmer or some example where they could at least understand how they could be used by and advanced programmer? Their mechanics are quite straight-forward for a beginning programmer to grasp, I would just like to find an example so that the learner could actually use them for a practical reason. Any ideas?

Here’s what I have so far, but it is just dry mechanics without any functionality:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //two ways to define them
            var customer = new Tuple<int, string, string>(23, "Sam", "Smith");
            var customer2 = Tuple.Create<int, string, string>(34, "James", "Allard");

            //with type inference, more concise (only available with the Create keyword)
            var customer3 = Tuple.Create(23, "John", "Hoopes");

            //you can go up to eight, then you have to send in another tuple
            var customer4 = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10));

            Console.WriteLine(customer.Item1);
            Console.WriteLine(customer2.Item2);
            Console.WriteLine(customer3.Item3);
            Console.WriteLine(customer4.Rest.Item1.Item3);

            Console.ReadLine();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:35:57+00:00Added an answer on May 16, 2026 at 5:35 am

    Tuples are not necessarily the first topic I would tackle for beginner programmers … however, there are some simple examples.

    One that comes to mind is returning a value (which may actually be null) from a function that performs a search (or calculation), together with a boolean that indicates whether the result was found or not. It’s an approach that avoids using out parameters, which can be cumbersome or problematic in certain scenarios (like LINQ queries):

    public Tuple<string,bool> SearchDictionary( string searchKey )
    {
        string value;
        bool wasFound = someDictionary.TryGetValue( searchKey, out value );
        return new Tuple<string,bool( value, wasFound );
    }
    
    // since <null> is a legal value to store in the dictionary, we cannot
    // use it to distinguish between 'value not found' and 'value is null'.
    // the Tuple<string,bool>, however, does allow us to do so...
    var result = SearchDictionary( "somekey" );
    if( result.Item2 )
    {
        Console.WriteLine( result.Item1 );
    }
    

    Another example that I think is natural, is creating associations between two values without creating an explicit class for the purpose. For example, let’s imagine we want to represent pairs of opponents who will play tennis matches. We could use:

    // note the symmetry in the representation of opponents of a tennis match...
    // if the relationship were asymmetrical, tuple may not be the best choice.
    var playerA  = new TennisPlayer("Serena Williams");
    var playerB  = new TennisPlayer("Venessa Williams");
    var match    = new Tuple<TennisPlayer,TennisPlayer>( playerA, playerB );
    

    Creating a class for something like this can be avoided by using tuples instead.

    A final example, is using tuples to represent composite keys in a dictionary. Since Tuple<>s can be compared to one another for equality, it becomes possible to do stuff like:

    var complexDictionary = 
          new Dictionary<Tuple<string,int,decimal,DateTime>,string>();
    complexDictionary.Add( new Tuple("USA",-4,1.671,DateTime.Now), "active" );
    

    EDIT: One comment I would make when educating developers about the use of tuples, is that tuples should rarely (if ever) appear in the public interface of code you expect others to consume. As tools to simplify the internal implementation of a class or module, I think they’re fine. But once you start passing them in or out of methods that developers consuming your code have to interact with, you run in the problem that tuples obscure the semantics of your API. It becomes hard for developers to interpret what Tuple<int,int> is supposed to mean. What do Item1 or Item2 mean in such a case? When you see yourself needing to pass tuples in or out of methods, you should strongly consider writing a class to encapsulate and clarify the relationship.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.