C
C#13mo ago
Aksword

❔ Permut array's values

Hello, is there a convenient way to swap array values in their array, for example transforming a {1,2,3,4} into a {1,4,2,3} I dunno. Because when I think of this I believe that a need to create a copy of the first array as a reference before swapping the values, but maybe there is a better way.
5 Replies
TravestyOfCode
TravestyOfCode13mo ago
You can save the first value in a temp variable, then assign the second value in the first value place, then assign the second location the value in temp.
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
ero
ero13mo ago
(array[i1], array[i2) = (array[i2], array[i1]);
(array[i1], array[i2) = (array[i2], array[i1]);
MODiX
MODiX13mo ago
thinker227#0000
REPL Result: Success
var xs = new[] {1,2,3,4,5};
(xs[1], xs[2]) = (xs[2], xs[1]);
xs
var xs = new[] {1,2,3,4,5};
(xs[1], xs[2]) = (xs[2], xs[1]);
xs
Result: int[]
[
1,
3,
2,
4,
5
]
[
1,
3,
2,
4,
5
]
Compile: 427.710ms | Execution: 58.419ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
Tuple swap FTW
Accord
Accord13mo 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
More Posts
❔ System.AccessViolationException from Windows.Media.Audio.AudioPlaybackConnection (UWP)For familiarity, AudioPlaybackConnection on UWP enables an A2DP sink Bluetooth profile connection be❔ Verify Uniqueness in an ArrayDoes anyone know how to determine if an array contain unique names in an array without using previou❔ ModMy sword is a little too big❔ CSharpSyntaxRewriter seems to not visit all types of SyntaxNodeHi, I'm trying to use `CSharpSyntaxRewriter` to refactor/rename a class. It was working fine when I ❔ Unable to Save Updated Data from Editable Table in ASP.NET MVCHey everyone, I hope you're all doing well. I'm reaching out to this amazing community seeking guid❔ [EF Core] Ignore properties from constructor of base classHey, I am currently trying to implement a new model in EF Core which looks like this: ```cs internal❔ [EF.Core] Discard external relations updates during concurrency conflictsHello, I'm in a situation where I have to handle a concurrency conflict inside an asp.net Core appli✅ Get this error while trying to Deserialize object``` An unhandled exception of type 'System.InvalidOperationException' occurred in System.Text.Json.d❔ Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'I'm having this exception thrown at run-time even though `builder.Services.AddHttpContextAccessor()`Blazor server vs. Blazor web assembly + Minimal APIIf my project can be created in any of the above. Which one should i go with for better future suppo