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 8157403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:16:23+00:00 2026-06-06T17:16:23+00:00

I would like to have a mapping function that does this: public static void

  • 0

I would like to have a mapping function that does this:

public static void Map<T>(IEnumerable<T> src, params T[] dst)
{
    for (int i = 0; i < dst.Length; i++)
        dst[i] = src.ElementAt(i);
}

In order to make this work dst would also have to be declared as ref, which doesn’t seem to be possible.

Here it is used in an imaginary unit test:

int a = 0, b = 0, c = 0;
int[] arr = { 1, 2, 3 };
Tools.Map(arr, a, b, c);
Assert.AreEqual(a, 1);
Assert.AreEqual(b, 2);
Assert.AreEqual(c, 3);

Is this possible at all? Does it already exist? Would it be a bad idea?

Edit: In other words, how do I give this implementation an arbitrary number of arguments:

public static void Map3<T>(IEnumerable<T> src, ref T a, ref T b, ref T c)
{
   a = src.ElementAt(0);
   b = src.ElementAt(1);
   c = src.ElementAt(2);
}
  • 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-06T17:16:25+00:00Added an answer on June 6, 2026 at 5:16 pm

    You can’t do that. Params is a convenience by the C# compiler, which just creates an array on the spot, and populates it with the arguments.

    Since an array cannot contain “ref” variables, neither can you pass “ref” to params in this way.

    You are looking for something like “multiple assignment” which some languages have. If it existed, it would work something like this:

    my (a,b,c,d) = enumerable.ToArray();
    

    See : C#: Split string and assign result to multiple string variables

    You might consider a set of extension methods:

    public static void ReadValues<T>(this T[] array, ref T t0){
       t0 = array[0];
    }
    public static void ReadValues<T>(this T[] array, ref T t0, ref T t1){
       t0 = array[0];
       t1 = array[1];
    }
    

    and so on, up to as many parameters as you want to support.

    It’s not an arbitrary number, but 20 should be high enough for most purposes. And hey, if it isn’t, just go higher.

    Then you can use it like this:

    enumerable.ToArray().ReadValues(var1, var2, var3);    
    

    You could even combine this with Slice to read arbitrary entries:

    // reads indexes 3 to 7
    enumerable.ToArray().Slice(3,4).ReadValues(var1, var2,var3,var4); 
    

    See slicing here:

    • Getting a sub-array from an existing array
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have an AppWidget that designed like this one . Image:
Imagine that I have a Spring MVC controller something like this: @Controller @RequestMapping(/base-url) public
I would like to have a mapping which maps two string into one string.
So in my project i would like have a nice treeview that has images.
I would like to have a border that is 4px thick pink on the
I would like to have a mapping to quickly quit all buffers, for use
I have a setup with 2GB of memory and I would like to map
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
have a Grails domain object that has a custom static function to grab data

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.