How Can I Replicate PHP Array Addition in C#?

  • C#
  • Thread starter adjacent
  • Start date
  • Tags
    Array
In summary, the conversation discusses the code in PHP that outputs multiples of 5 in the first 100, and how to reproduce it in C# using the List class. The code is later improved to use an array and to output the numbers without storing them.
  • #1
adjacent
Gold Member
1,552
63
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
  • #3
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);
                }
            }
 
  • #4
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?
 
  • #5


In C#, you can add values to an array using the .Add method. Here is an example of how to reproduce the code in the question using C#:

int[] out = new int[100]; //create an array with 100 elements
for (int i = 0; i < 100; i++)
{
if (i % 5 == 0)
{
out.Add(i); //using the .Add method to add values to the array
}
}
foreach (int val in out)
{
Console.WriteLine(val); //printing out the values in the array
}

The key difference in C# is that you need to specify the size of the array beforehand, whereas in PHP, arrays can dynamically grow as you add values to them.
 

1. How do I add a single value to an array in C#?

To add a single value to an array in C#, you can use the Array.Resize method. First, you will need to create a new array with a larger size than the original array. Then, you can use the Array.Copy method to copy the original array into the new array and finally add the new value to the end of the array.

2. Can I add multiple values at once to an array in C#?

Yes, you can add multiple values at once to an array in C# using the Array.AddRange method. This method takes in an existing array and a new array of values to add, and it will combine the two arrays into one.

3. How can I add values to a specific index in an array in C#?

To add values to a specific index in an array in C#, you can use the Array.Insert method. This method takes in the array, the index where you want to add the value, and the value itself as parameters. It will then shift all the elements after the specified index to make room for the new value.

4. Is it possible to add values to the beginning of an array in C#?

Yes, you can add values to the beginning of an array in C# using the Array.Insert method. You will need to specify an index of 0 to indicate that you want to add the value at the beginning of the array.

5. What happens if I try to add a value to an array that is already full in C#?

If you try to add a value to an array that is already full in C#, you will encounter an IndexOutOfRangeException error. This means that the array does not have enough space to accommodate the new value. To add the value, you will need to resize the array using the Array.Resize method before adding the new value.

Similar threads

  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
6
Views
975
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
13
Views
3K
Back
Top