C# Console CursorTop is not calculated as same as native windows cmd #13763

Open
opened 2026-01-31 03:51:29 +00:00 by claunia · 0 comments
Owner

Originally created by @MohammadMD1383 on GitHub (May 9, 2021).

Windows Terminal version (or Windows build number)

Windows10 - 20H2 - 19042.964

Other Software

No response

Steps to reproduce

  1. write this program in c#
using System;
using static System.Console;

namespace ConsoleApp
{
	internal static class Program
	{
		public static void Main()
		{
			Write("Hello World");
			ClearLine(CursorTop);
			SetCursorPosition(0, CursorTop - 1); // exception here
		}

		private static void ClearLine(int lineNumber)
		{
			SetCursorPosition(0, lineNumber);
			Write("".PadRight(WindowWidth));
		}
	}
}
  1. compile
  2. run it from windows terminal powershell using .\ConsoleApp.exe

Expected Behavior

it should fill the current line with white space and then because the white spaces count is equal to windows width, after the last white space the cursor should move to next line
then by setting cursor top to cursor top - 1 it shouldn't throw any exception.

I tested this in cmd and powershell and there was no problem.

Actual Behavior

after last white space cursor doesn't go to next line so it throws exception.
image

Originally created by @MohammadMD1383 on GitHub (May 9, 2021). ### Windows Terminal version (or Windows build number) Windows10 - 20H2 - 19042.964 ### Other Software _No response_ ### Steps to reproduce 1. write this program in c# ```csharp using System; using static System.Console; namespace ConsoleApp { internal static class Program { public static void Main() { Write("Hello World"); ClearLine(CursorTop); SetCursorPosition(0, CursorTop - 1); // exception here } private static void ClearLine(int lineNumber) { SetCursorPosition(0, lineNumber); Write("".PadRight(WindowWidth)); } } } ``` 2. compile 3. run it from windows terminal powershell using `.\ConsoleApp.exe` ### Expected Behavior it should fill the current line with white space and then **because the white spaces count is equal to windows width, after the last white space the cursor should move to next line** then by setting cursor top to `cursor top - 1` it shouldn't throw any exception. I tested this in `cmd` and `powershell` and there was no problem. ### Actual Behavior after last white space cursor doesn't go to next line so it throws exception. ![image](https://user-images.githubusercontent.com/69088224/117583427-61074880-b11c-11eb-9385-5d7bef9ee00d.png)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13763