C
C#2y ago
Ice_

Arrays in constructors and lists

I have made a constructor taking an array of int (int[] numbers) in a new class, let's call the class Digits. I have made a public property to an array of int in the class called num (with get; set;). I then make a new object in my main class and assign some numbers, three of them: Digits tmpobj = new(new int[] { 16,23,51 }); I then try to make a list: List<Digits> myList = new List<Digits>(); myList.Add(tmpobj); If I do a Console.Writeline(tmpobj.num[0]); it will return 16. If I increment it with one I get 23 etc. I then do a foreach (var item in myList). I've done a breakpoint after that and I see that myList in this case shows [0] .num[0] = 16, next 23 etc. Then I've done a Console.Writeline after each item and it's just returning the int32 stuff, I thought the "Add" method would add the full array to the list but it does not. I can see that stuff isn't right but I'm not sure how to fix this at the moment. Please help 😅
13 Replies
ero
ero2y ago
the Add method doesn't add an array to the list it adds an instance of Digits to the list
Ice_
Ice_2y ago
Ooooh. Of course! It's an object. Makes sense. Thanks!
ero
ero2y ago
if you want, you could override ToString
Ice_
Ice_2y ago
I was thinking of that but I really just wanted to try it the hard way 😅 List can be used to add, let's say integers or strings as well. How would I create a list from tmpobj declared at top? Its integers in mind
ero
ero2y ago
i'm not sure what you mean
Ice_
Ice_2y ago
Hm nevermind. I guess foreach is only for lists?
ero
ero2y ago
foreach is for anything that implements IEnumerable anything that you can... enumerate if you want to enumerate over an instance of Digits, you'd have to have that class implement IEnumerable<int> (presumably)
Ice_
Ice_2y ago
Hm okay, that is a bit over my expertise right now 😝 I'm aware IEnumerable is an (interface?) but I don't know much about it. So in my case, foreach isn't exactly ideal to write out the integer array from the class object?
ero
ero2y ago
it's just not even possible unless you do foreach (int i in tmpobj.num)
Ice_
Ice_2y ago
Ah... Thanks a lot! Makes great sense. I think I was confused about list, object and datatypes!
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
Digits dgts = new(42, 123, 1337);

foreach (int i in dgts)
{
Console.WriteLine(i);
}

class Digits : IEnumerable<int>
{
private readonly int[] _nums;

public Digits(params int[] nums)
{
_nums = nums;
}

public IEnumerator<int> GetEnumerator()
{
return ((IEnumerable<int>)_nums).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return _nums.GetEnumerator();
}
}
Digits dgts = new(42, 123, 1337);

foreach (int i in dgts)
{
Console.WriteLine(i);
}

class Digits : IEnumerable<int>
{
private readonly int[] _nums;

public Digits(params int[] nums)
{
_nums = nums;
}

public IEnumerator<int> GetEnumerator()
{
return ((IEnumerable<int>)_nums).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return _nums.GetEnumerator();
}
}
Console Output
42
123
1337
42
123
1337
Compile: 664.115ms | Execution: 75.350ms | React with ❌ to remove this embed.
ero
ero2y ago
love ya modix never fail to disappoint me with the goddamn indentations
Ice_
Ice_2y ago
Great post! Impressive bot btw!