C
C#2y 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
RonnieOP2y ago
oh
Angius
Angius2y ago
arr.ToList() should also work, IIRC
Ronnie
RonnieOP2y 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
Angius2y ago
Or just foreach ¯\_(ツ)_/¯
MODiX
MODiX2y 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
Angius2y ago
This does not create a list from an array
Jester
Jester2y ago
you have a list without needing an array
Ronnie
RonnieOP2y ago
the for loop i made looping through doesnt seem to work
Angius
Angius2y ago
What doesn't work about it?
Ronnie
RonnieOP2y 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
MODiX2y 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
Angius2y ago
.Count
MODiX
MODiX2y 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
Angius2y ago
Alternatively...
Ronnie
RonnieOP2y ago
what is .cout .count*
Angius
Angius2y ago
A property
PatrickG
PatrickG2y ago
int[] arr = { 1, 2, 3 }; List<int> list = new List<int>(); list.AddRange(arr);
Ronnie
RonnieOP2y ago
oh ok
MODiX
MODiX2y 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
Angius2y ago
Use a foreach and never worry about Count/Length again
Ronnie
RonnieOP2y ago
ah that works too ok welp thanks everyone
Jester
Jester2y ago
see he doesnt need an array but just a list
Ronnie
RonnieOP2y ago
no i was converting the arr into a list then looping through
Jester
Jester2y ago
you can also loop trough an array
Ronnie
RonnieOP2y ago
im guessing that i shouldn't use lists unless i really need them huh
Jester
Jester2y ago
yeah
Ronnie
RonnieOP2y ago
ah ok
Jester
Jester2y ago
each type has their use
Angius
Angius2y ago
Lists are used when you need a dynamically-sized collection, so .Add(), .Remove(), etc
Ronnie
RonnieOP2y ago
alright
MODiX
MODiX2y 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
Accord2y 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.
Want results from more Discord servers?
Add your server