C
C#3y ago
Ronnie

❔ ✅ C# lists

int[] arr = {1, 2, 3, 4, 5};
List<int> myList = new List<int>();
int[] arr = {1, 2, 3, 4, 5};
List<int> myList = new List<int>();
rather than using .Add() is there a way that i can put the array into myList in order to create the list from that
32 Replies
Ronnie
RonnieOP3y ago
oh
Angius
Angius3y ago
arr.ToList() should also work, IIRC
Ronnie
RonnieOP3y ago
thanks guys what is i wanted to loop through the list, would i use arr.length or myList.length? @implicit electron huh
for (int i = 0; i < myList.length; i++)
{
Console.WriteLine(myList[i]);
}
for (int i = 0; i < myList.length; i++)
{
Console.WriteLine(myList[i]);
}
Angius
Angius3y ago
Or just foreach ¯\_(ツ)_/¯
MODiX
MODiX3y ago
Jester#8471
REPL Result: Success
List<int> myList = new() { 1, 2, 3, 4, 5 };
myList
List<int> myList = new() { 1, 2, 3, 4, 5 };
myList
Result: List<int>
[
1,
2,
3,
4,
5
]
[
1,
2,
3,
4,
5
]
Compile: 564.553ms | Execution: 34.230ms | React with ❌ to remove this embed.
Angius
Angius3y ago
This does not create a list from an array
Gooster
Gooster3y ago
you have a list without needing an array
Ronnie
RonnieOP3y ago
the for loop i made looping through doesnt seem to work
Angius
Angius3y ago
What doesn't work about it?
Ronnie
RonnieOP3y ago
using System;
using System.Collections.Generic;

namespace CSharp
{
class Program
{

public class myClass
{
public string colour = "Purple";
}

static void Main(string[] args)
{
myClass Obj = new myClass();
Console.WriteLine(Obj.colour);


int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Length; i++)
{
Console.WriteLine(myList[i]);
}

}
}
}
using System;
using System.Collections.Generic;

namespace CSharp
{
class Program
{

public class myClass
{
public string colour = "Purple";
}

static void Main(string[] args)
{
myClass Obj = new myClass();
Console.WriteLine(Obj.colour);


int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Length; i++)
{
Console.WriteLine(myList[i]);
}

}
}
}
ignore the class
MODiX
MODiX3y ago
Angius#1586
REPL Result: Failure
int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Length; i++)
{
Console.WriteLine(myList[i]);
}
int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Length; i++)
{
Console.WriteLine(myList[i]);
}
Exception: CompilationErrorException
- 'List<int>' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?)
- 'List<int>' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?)
Compile: 661.372ms | Execution: 0.000ms | React with ❌ to remove this embed.
Angius
Angius3y ago
.Count
MODiX
MODiX3y ago
Angius#1586
REPL Result: Success
int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(myList[i]);
}
int[] arr = {1, 2, 3, 4, 5};

List<int> myList = new List<int>(arr);

for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(myList[i]);
}
Console Output
1
2
3
4
5
1
2
3
4
5
Compile: 641.923ms | Execution: 70.760ms | React with ❌ to remove this embed.
Angius
Angius3y ago
Alternatively...
Ronnie
RonnieOP3y ago
what is .cout .count*
Angius
Angius3y ago
A property
PatrickG
PatrickG3y ago
int[] arr = { 1, 2, 3 }; List<int> list = new List<int>(); list.AddRange(arr);
Ronnie
RonnieOP3y ago
oh ok
MODiX
MODiX3y ago
Angius#1586
REPL Result: Success
int[] arr = {1, 2, 3, 4, 5};
List<int> myList = new List<int>(arr);

foreach (var element in myList)
{
Console.WriteLine(element);
}
int[] arr = {1, 2, 3, 4, 5};
List<int> myList = new List<int>(arr);

foreach (var element in myList)
{
Console.WriteLine(element);
}
Console Output
1
2
3
4
5
1
2
3
4
5
Compile: 607.012ms | Execution: 71.563ms | React with ❌ to remove this embed.
Angius
Angius3y ago
Use a foreach and never worry about Count/Length again
Ronnie
RonnieOP3y ago
ah that works too ok welp thanks everyone
Gooster
Gooster3y ago
see he doesnt need an array but just a list
Ronnie
RonnieOP3y ago
no i was converting the arr into a list then looping through
Gooster
Gooster3y ago
you can also loop trough an array
Ronnie
RonnieOP3y ago
im guessing that i shouldn't use lists unless i really need them huh
Gooster
Gooster3y ago
yeah
Ronnie
RonnieOP3y ago
ah ok
Gooster
Gooster3y ago
each type has their use
Angius
Angius3y ago
Lists are used when you need a dynamically-sized collection, so .Add(), .Remove(), etc
Ronnie
RonnieOP3y ago
alright
MODiX
MODiX3y ago
Jester#8471
REPL Result: Success
int[] arr = {1, 2, 3, 4, 5};

foreach (var element in arr)
{
Console.WriteLine(element);
}
int[] arr = {1, 2, 3, 4, 5};

foreach (var element in arr)
{
Console.WriteLine(element);
}
Console Output
1
2
3
4
5
1
2
3
4
5
Compile: 595.764ms | Execution: 66.240ms | React with ❌ to remove this embed.
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?