How Can I Replicate PHP Array Addition in C#?

  • Context: C# 
  • Thread starter Thread starter adjacent
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around replicating PHP array addition in C#, specifically how to collect multiples of a number within a specified limit. The scope includes programming techniques and efficiency considerations in C#.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a PHP code snippet that collects multiples of 5 from 0 to 100 and asks how to replicate the array addition in C#.
  • Another participant suggests using the List class in C# and provides a code example demonstrating how to add elements to a list.
  • A later reply presents a modified version of the code that allows user input for the limit and multiple, utilizing a List to store the results.
  • Another participant critiques the efficiency of the provided code, suggesting that many iterations of the loop do not yield useful results and questions whether an array could be used instead of a list.
  • This participant also raises the point of whether storing the numbers is necessary if the goal is simply to output them.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of the code and the choice between using a List or an array. The discussion remains unresolved regarding the best approach to achieve the desired functionality.

Contextual Notes

There are limitations regarding the efficiency of the loop and the necessity of storing results, which remain unaddressed in terms of definitive solutions.

adjacent
Gold Member
Messages
1,552
Reaction score
62
The title.
A friend of me told me the code in php
Code:
<?php 
for($i=0; $i<=100; $i++) 
{ 
     if($i%5==0) 
     { 
          $out[] = $i; 
     } 
}  
foreach($out as $val) 
{ 
   echo $val."<br>"; 
} 
?>
The result is the multiples of 5 in the first 100.
How do I reproduce this part:"$out[] = $i; " in C#?
I have searched everywhere but couldn't find how to do that.
 
Last edited:
Technology news on Phys.org
DrZoidberg said:
Use the List class.
http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

Code:
List<int> out= new List<int>();
...
out.Add(i);
Oh, thank you so much. This is my final code :smile:
Code:
while (true)
            {
                int limit;
                int multiple;
                Console.Write("Limit:");
                int.TryParse(Console.ReadLine(), out limit);
                Console.Write("Multiple:");
                int.TryParse(Console.ReadLine(), out multiple);
                List<int> no = new List<int>();
                for (int i = 1; i <= limit; i++)
                {
                    if (i % multiple == 0)
                    {
                        no.Add(i);
                    }
                }

                foreach (object o in no)
                {
                    Console.WriteLine(o);
                }
            }
 
This code is inefficient. Out of many iterations of the loop, only a small number produce a useful result. Can you improve that, ideally so that every iteration results in a addition to the list?

Further, can you use an array, as you seem to have wanted initially, rather than a list?

Finally, if the goal is to output the numbers, do you really need to store them?
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 13 ·
Replies
13
Views
5K