Can I add elements to array by Console.ReadLine?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Can I add elements to array by Console.ReadLine??

About Arrays

20th Aug 2016, 7:11 PM
Nurlan Aliyev
Nurlan Aliyev - avatar
5 Answers
+ 3
//declare array here //add elements here: for(int i = 0; i < arr.Length; i++) array[i] = Console.ReadLine();
21st Aug 2016, 6:59 PM
Erwin Mesias
Erwin Mesias - avatar
+ 1
yes. pass the elements in a variable and then pass that variable into the array.do this in a for loop
21st Aug 2016, 10:09 AM
Suahant
+ 1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int[ ] a = new int[10]; for (int k = 0; k < 10; k++) { Console.WriteLine("enter the {0} element",k); a[k]=Convert.ToInt32(Console.ReadLine()); } for (int k = 0; k < 10; k++) { Console.WriteLine(a[k]); } } } }
14th Oct 2016, 9:19 AM
mohamed kacem
mohamed kacem - avatar
0
Thank u i got it
21st Aug 2016, 10:11 AM
Nurlan Aliyev
Nurlan Aliyev - avatar
0
keep in mind you can only add a predetermined amount of items - for i < array.length; if you wanted to maintain adding things, that's where List comes in handy.
4th Sep 2016, 5:38 AM
Linas Tomas Matiukas
Linas Tomas Matiukas - avatar