deconstruct array into variables?

pythonatSheriff

Full Stack Web Developer
Joined
Dec 15, 2021
Messages
25
Location
Egypt
Programming Experience
1-3
I have an array of unlimited number of strings and I need to deconstruct this array into variables, so I searched for built-in method but in vain, so I decided to make my own function :
C#:
 public static void Deconstruct(this string[] strArr, out params string)
                {
                 for (int i = 0; i < strArr.Length; i++)
                     {
                         s[i] = strArr[i];
                     }

                }
There is something wrong in using "params" as out , how can I make such a function, please ?
 
Last edited by a moderator:
I don't know if it's ok to share github accounts right here , but here is mine :
--www.github.com/shiccorama
--https://www.linkedin.com/in/sherif-fawzy-07111980
--https://twitter.com/shiccorama?t=UBd_iK96EQ0OukQ2K6k3Xw&s=09
 
it will be a great pleasure to tell me why ( to keep in mind for next cases )
Knowing all the requirements ahead of time makes planning easier.

It's like being told, compute the shortest distance between point A and point B given the longitude and latitude of the two points on the globe. And so you go find the formula to compute the spherical distance between the two.

And then you get told later you are told you may only cross water features three times at most.
 
The LINQ that JohnH matches up exactly with what I suggested about building the string. All the looping just gets hidden away by the LINQ.

Where() loops over items and filters the items.
OrderBy() loops over the items and returns them in sorted order.
Join() loops over the items and feeds them into a StringBuilder to assemble them with separators, and return a single string the string builder.

Assuming you use the SortedDictionary suggested above, there is no need for the OrderBy(). So using just for/foreach loops:
C#:
var dictionary = new SortedDictionary<string, int>();

// var filtered = dictionary.Where(kvp => kvp.Value != 0);
var filtered = new List<KeyValuePair>();
foreach(var kvp in dictionary)
{
    if (kvp.Value != 0)
        filtered.Add(kvp);
}

// var pairs = filtered.Select(kvp => $"{kvp.Key}:{kvp.Value}");
var pairs = new List<string>()
foreach(var kvp in filtered)
{
    pairs.Add($"{kvp.Key}:{kvp.Value}");
}

// var result = string.Join(",", pairs);
var sb = StringBuilder();
bool first = true;
foreach(var pair in pairs)
{
    if (first)
        first = false;
    else
        sb.Append(",");
    sb.Append(pair);
}
var result = sb.ToString();

And if you wanted to do just one loop:
C#:
var sb = StringBuilder();
bool first = true;
foreach(var kvp in dictionary)
{
    if (kvp.Value == 0)
        continue;

    if (first)
        first = false;
    else
        sb.Append(",");

    sb.Append($"{kvp.Key}:{kvp.Value}");
}
var result = sb.ToString();
 
Last edited:
Loop-Integrated-Query is not what it stands for, but... could have been.
 
I am so happy to meet you guys and I really appreciate every single moment you spent on teaching me and I promise to spend more time studying and searching before asking for help. Finally, I would like to ask for a general advice, what should I do to reach your level 😊, beside practicing of course?
 
Also take time to read other people's code. Read blog posts. Try re-writing your old code. Explore other languages.
 
Back
Top Bottom