|Have you tried stepping through this code with a debugger so you know what each line is doing? The debugger will allow you to execute one line at the time and inspect all variable values as you do so.
When you are new to coding, debugging might seem to be an advanced topic you will learn later. This is a huge mistake. debugging is simple:
Tutorial: Debug C# code - Visual Studio (Windows) | Microsoft Learn[^] (if you do not use Visual Studio Google "tutorial debugging c# with xxxx" where xxxx is whatever you use to write code.
No matter if you are a professional or a beginner, the debugger is the tool that will save you the most time by far.