C
C#ā€¢2y ago
a coding witch

āœ… Help in 2d matrix

var lines = Enumerable.Repeat(1, 4).ToArray();
var columns = Enumerable.Repeat(lines, 4).ToList();
columns[0][0] = 2;
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(columns));

//[[2,1,1,1],[2,1,1,1],[2,1,1,1],[2,1,1,1]]
var lines = Enumerable.Repeat(1, 4).ToArray();
var columns = Enumerable.Repeat(lines, 4).ToList();
columns[0][0] = 2;
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(columns));

//[[2,1,1,1],[2,1,1,1],[2,1,1,1],[2,1,1,1]]
Why does it changes all the inner arrays instead of the first one kekw
25 Replies
Angius
Angiusā€¢2y ago
Array is a reference type
a coding witch
a coding witchā€¢2y ago
ahbn
Angius
Angiusā€¢2y ago
You're filling the columns with 4 references to the same lines array
a coding witch
a coding witchā€¢2y ago
blobSpoop hey how to enumerable repeat by catbruh
Servator
Servatorā€¢2y ago
What do you want to do ?
Angius
Angiusā€¢2y ago
I guess you could try a LINQ trick... maybe
a coding witch
a coding witchā€¢2y ago
im in caffeine overload, little bugged brain
Angius
Angiusā€¢2y ago
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
a coding witch
a coding witchā€¢2y ago
but Hmmmm thanks
Angius
Angiusā€¢2y ago
arr.ToArray() should create a copy
a coding witch
a coding witchā€¢2y ago
jusst toarray didnt work ill try this select one oh your solution did work! thanks whats your gh @? ill lik you as co author
MODiX
MODiXā€¢2y ago
Angius#1586
REPL Result: Success
var a1 = new int[]{1, 2, 3};
var a2 = Enumerable
.Repeat(0, 4)
.Select(_ => a1.ToArray())
.ToList();

a2[1] = new int[]{4, 5, 6};

a2
var a1 = new int[]{1, 2, 3};
var a2 = Enumerable
.Repeat(0, 4)
.Select(_ => a1.ToArray())
.ToList();

a2[1] = new int[]{4, 5, 6};

a2
Result: List<int[]>
[
[
1,
2,
3
],
[
4,
5,
6
],
[
1,
2,
3
],
[
1,
2,
3
]
]
[
[
1,
2,
3
],
[
4,
5,
6
],
[
1,
2,
3
],
[
1,
2,
3
]
]
Compile: 571.554ms | Execution: 83.665ms | React with āŒ to remove this embed.
MODiX
MODiXā€¢2y ago
Samarichitane#3472
REPL Result: Success
var board = Enumerable.Repeat('x', 64).Chunk(8);

board
var board = Enumerable.Repeat('x', 64).Chunk(8);

board
Result: <ChunkIterator>d__69<char>
[
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
]
]
[
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
]
]
Compile: 531.205ms | Execution: 34.442ms | React with āŒ to remove this embed.
Angius
Angiusā€¢2y ago
@Atulin Huh, that .Chunk() solution is smart
a coding witch
a coding witchā€¢2y ago
thanks
Servator
Servatorā€¢2y ago
You can close thread with /close šŸ‘
a coding witch
a coding witchā€¢2y ago
\close
Servator
Servatorā€¢2y ago
not \ with /
a coding witch
a coding witchā€¢2y ago
waht a fuck
Angius
Angiusā€¢2y ago
huh Ah, wrong bot's command
Angius
Angiusā€¢2y ago
Pick the Accord one
a coding witch
a coding witchā€¢2y ago
i did but i still can post šŸ§
Servator
Servatorā€¢2y ago
Try again
a coding witch
a coding witchā€¢2y ago
a coding witch
a coding witchā€¢2y ago
catgun
Want results from more Discord servers?
Add your server
More Posts
ā” Compilation option questionsI am currently reading through different options that can be set inside a .csproj file, as well as oā” ContextSwitchDeadlockHi - in my windows form program I generate some RSA keys before the program loads, this takes aroundā” is there any way i can run my csharp console app net framwork program executable on my gpu?Hi, i want to know if there is any way i can run my c# console app net framwork executable file on mā” How to get the type that got inheri ?I got a list of PetProfileCardsGroupModel Is there a way I can get a List<PetProfileCardModel> from ā” edit - how do i map the output of EnumerateAudioEndPoints to the wave device numbers?when i have an endpoint notification callback registered, and try to dispose any sort of waveout or ā” Winforms Property from BaseClass Form not getting transferred to derived FormHi. Don't know if you guys can help an amateur. I know most of you guys don't like winforms, but... ā” Repeating parameters vs variadic functionsDoes anybody know why the `params` keyword is not used more commonly? This example if from the `Immuā” does an interface go inside a generic parent class?```csharp using Godot; using System; public class StateTemplate : Node { public enum State { ā” Copying video on Delegates, why doesn't my version work?Why does my code not work, but the code in the video i'm following does. <:OhNo:632294461048881222>ā” how can WPF change the hover mouseover color of something like Winforms can?I'm getting increasingly frustrated at how many features WPF is just lacking compared to winforms, a