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

  • SEARCH
  • Home
  • 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 8133897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:48:30+00:00 2026-06-06T09:48:30+00:00

Disclaimer: this question is driven by my personal curiosity more than an actual need

  • 0

Disclaimer: this question is driven by my personal curiosity more than an actual need to accomplish something. So my example is going to be contrived.
Nevertheless I think it’s an issue that might very well crop up.

Let’s say we are using Zip to iterate over two sequences, invoking a void method that just throws an exception if one item of the couple is found to be different from the other (therefore discarding any return value). The point here is not that the method throws an exception, so much as it returns void.

In other words, we’re kind of doing a ForEach over two collections (and by the way, I know what Eric Lippert thinks about ForEach, and fully agree with him and never use it).

Now, Zip wants a Func<TFirst, TSecond, TResult>, so of course passing something equivalent to Action<TFirst, TSecond> won’t work.

My question is: is there an idiomatic way that is better than this (i.e. returning a dummy value)?

var collection1 = new List<int>() { ... };
var collection2 = new List<int>() { ... };

collection1.Zip(collection2, (first, second) => 
{
    VoidMethodThatThrows(first, second);
    return true;
});
  • 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-06-06T09:48:31+00:00Added an answer on June 6, 2026 at 9:48 am

    Use Zip() to throw the items into an object, then do your foreach however way you choose (do a normal foreach loop please, not the bad ToList/ForEach combo).

    var items = collection1.Zip(collection2, (x, y) => new { First = x, Second = y });
    foreach (var item in items)
    {
        VoidMethodThatThrows(item.First, item.Second);
    }
    

    As of C# 7.0, improved tuple support and deconstruction makes it far more pleasing to work with.

    var items = collection1.Zip(collection2, (x, y) => (x, y));
    // or collection1.Zip(collection2, ValueTuple.Create);
    foreach (var (first, second) in items)
    {
        VoidMethodThatThrows(first, second);
    }
    

    Furthermore, .NET Core and 5 adds an overload which automatically pairs the values into tuples so you don’t have to do that mapping.

    var items = collection1.Zip(collection2); // IEnumerable<(Type1, Type2)>
    

    .NET 6 adds a third collection to the mix.

    var items = collection1.Zip(collection2, collection3); // IEnumerable<(Type1, Type2, Type3)>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.
Disclaimer: This question is strictly academic. The example I'm about to give is probably
DISCLAIMER: This is not a real-world example. It is just a theoretical question of
Disclaimer : this is not a how to question. I would more like to
(disclaimer: this question is much simpler than the title suggests!) I have a re-occuring
Disclaimer: this question is purely informational and does not represent an actual problem I'm
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This question is not about fixing visual studio So, I've used VSS for
( Disclaimer: This question is not specific to ASP.NET) I have a control which
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less

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.