Skip to content

C# ​

Version ​

Running C# on .NET 8.0

Supported languages ​

C#

Testing framework ​

xUnit v2.7.0

Special reminders and implementation details ​

csharp
using Xunit;

namespace Challenge
{
    public class HelloWorldTest
    {
        [Fact(DisplayName = "It should return 'Hello World!'")]
        public void TestHelloWorld()
        {
            Assert.Equal("Hello World!", HelloWorld.Hello());
        }
    }
}

Included libraries ​

Reading input (stdin) ​

Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.

csharp
using System;

namespace Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = Console.ReadLine();
            Console.WriteLine($"Hello, {name}!");
        }
    }
}

Console.ReadLine() returns one line without its newline and null at end-of-file. Do not print a prompt before reading in output-test challenges: it would become part of the compared output.

How to debug ​

Debugging C# code is essential for every developer. One of the simplest yet effective ways to debug is by using Console.WriteLine statements.

Steps to Debug Using Console.WriteLine Statements ​

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert Console.WriteLine Statements: Add Console.WriteLine statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests.

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here's an example of how to use Console.WriteLine for debugging:

csharp
namespace Challenge
{
    public class Calculator
    {
        public static int CalculateSum(int a, int b)
        {
            Console.WriteLine($"CalculateSum called with arguments: {a}, {b}"); // Log function call
            int sum = a + b;
            Console.WriteLine($"Sum after addition: {sum}"); // Log sum calculation
            return sum;
        }
    }
}