Using ChatGPT for C# Development

Writing C# Code with ChatGPT

I have another post on creating a Trivia Application using ChatGPT. It is an ongoing story on how I use ChatGPT to accelerate the development of an ASP.Net C# web-based trivia application. Trivia Spark:Igniting Creativity With ChatGPT

What is ChatGPT

ChatGPT (short for Chat Generative Pre-trained Transformer) is a natural language processing (NLP) model developed by OpenAI . OpenAI is an artificial intelligence (AI) research laboratory. It is designed to be able to generate human-like responses to text input and has been trained on a large dataset of text in order to learn the patterns and nuances of human language.

The language model can answer questions, and assist you with tasks such as composing emails, essays, and code. Usage is currently open to the public free of charge because ChatGPT is in its research and feedback-collection phase.

While some of the underlying technology behind ChatGPT is open source, the specific model and its trained parameters are likely proprietary to OpenAI. Currently, ChatGPT is only available as a cloud-based service and is not available for installation on an internal network.

ChatGPT learns through a process called training, where it is fed a large dataset of text and uses this data to learn patterns and relationships in language. It then uses this knowledge to generate responses to new text input.

For more information on ChatGPT, see the OpenAI Blog . or just watch the video below.

ChatGPT for C# Developers

There has been much chatter about the new Artificial intelligence tool and how it will impact teachers, artists, writers, and web search. For this post, I wanted to look at the tool from a developer's perspective.

ChatGPT can be used to improve developer productivity. It can be used to answer technical questions and provide information about the development process.

Not every response from ChatGPT is correct or even runnable. It does however provide another tool for the developer to use to improve their productivity. Along with tools like GitHub Co-Pilot and Stack Overflow, developers are now to be more productive.

Explaining existing code blocks

One of the most helpful ways I've used ChatGPT is to ask it to explain code that I don't fully understand. This can be especially useful when working with code written by someone else, or when dealing with particularly convoluted code. Here's an example of how ChatGPT can explain some C# code:

// Input:
// Can you explain this C# code to me?
public class MyClass
{
  public int Property { get; set; }
}
// Output:
// This code defines a class called "MyClass" with a single property called "Property".
// The property is of type "int" and is publicly accessible using the "get" and "set" accessors.
Improve existing code

Another useful feature of ChatGPT is its ability to help you improve existing code. Simply describe what you want to accomplish, and ChatGPT will provide you with instructions on how to do it, including the modified code. For example:

    Can you help me refactor this code to make it more readable?
    // This code defines a function that sorts an array of integers
    public void QuickSort(int[] array)
    {
        if (array.Length <= 1) return;
        int pivot = array[array.Length / 2];
        int left = 0;
        int right = array.Length - 1;
        while (left <= right)
        {
            while (array[left] < pivot) left++;
            while (array[right] > pivot
        }
    }

This is what Chat GPT returns:

    public void QuickSort(int[] array)
        {
            if (array.Length <= 1)
            {
                return;
            }

            int pivot = array[array.Length / 2];
            int left = 0;
            int right = array.Length - 1;

            while (left <= right)
            {
                while (array[left] < pivot)
                {
                    left++;
                }

                while (array[right] > pivot)
                {
                    right--;
                }

                if (left <= right)
                {
                    int temp = array[left];
                    array[left] = array[right];
                    array[right] = temp;
                    left++;
                    right--;
                }
            }

            QuickSort(array.Take(right + 1).ToArray());
            QuickSort(array.Skip(left).ToArray());
        }
        

This refactored version includes:

  • Braces around all of the code blocks to make the structure of the code more explicit
  • Variable names that are more descriptive of their purpose
  • White space to separate logical blocks of code

The refactored version is more readable and maintainable for future developers.

Rewriting code for style

It can be frustrating to work with code that doesn't follow the usual conventions and standards. It can be especially challenging when the code was written by a junior C# developer who may be unfamiliar with the best practices and conventions of the language.

Fortunately, ChatGPT can help you rewrite code to follow the correct style and conventions. Simply describe what you want to accomplish and ChatGPT will provide you with the updated code, along with explanations for the changes it made.

Here's an example of how ChatGPT can help you rewrite code to follow the correct style:

// Input:
// Can you help me rewrite this code to follow C# naming conventions?
// This code defines a class with an improperly named property and method
public class MyClass
{
  public int Prop { get; set; }
  public void Do_Something()
  {
    // Method body
  }
}

// Output:
// This code defines a class with a properly named property and method
public class MyClass
{
  public int Prop { get; set; }

  public void DoSomething()
  {
    // Method body
  }
}

As you can see, ChatGPT correctly renamed the Prop property to follow the camelCase naming convention and renamed the Do_Something method to remove the underscore and use PascalCase instead.

By using ChatGPT to rewrite code to follow the correct style, you can save time and effort and improve the readability and maintainability of your code.

Rewriting code with idiomatic constructs

ChatGPT can be particularly helpful when reviewing and refactoring code written by inexperienced C# developers. One of the challenges of working with existing code is that it may not always follow the best practices and conventions of the language. This can make the code more difficult to read and maintain, and it can take more time and effort to review and refactor it.

You can use ChatGPT to rewrite code using idiomatic constructs to ensure that your code follows the best practices and conventions of C#, making it more readable and maintainable.

Here's an example of how ChatGPT can help you rewrite code using idiomatic constructs:

// Input:
// Can you help me rewrite this code to use C# idiomatic constructs?

// This code defines a function that uses a non-idiomatic loop to sum the elements of an array
public int SumArray(int[] array)
{
  int sum = 0;
  for (int i = 0; i < array.Length; i++)
  {
    sum += array[i];
  }
  return sum;
}

// Output:
// This code defines a function that uses an idiomatic loop to sum the elements of an array
public int SumArray(int[] array)
{
  int sum = 0;
  foreach (int element in array)
  {
    sum += element;
  }
  return sum;
}

In this example, ChatGPT changed the loop from a traditional for loop to a for each loop, which is more idiomatic in C#. This not only makes the code more readable but makes it more concise and easier to understand.

By using ChatGPT to rewrite code using idiomatic constructs, you can ensure that your code follows best practices and is easier to read and maintain. This can save you hours of work every day, and it can help you stay competitive with other developers who are using AI effectively to improve their efficiency.

Simplifying existing code

ChatGPT can be used to simplify C# code by suggesting alternative implementations that are more efficient or easier to understand. You could prompt ChatGPT with the following:

    Rewrite the following C# method to be more efficient:

        public double Average(List<int> numbers)
        {
            double sum = 0;
            foreach (int number in numbers)
            {
                sum += number;
            }
            return sum / numbers.Count;
        }
        

ChatGPT might suggest the following alternative implementation, which uses the Sum() method of the List of int class to more efficiently calculate the sum of the numbers:

    public double Average(List<int> numbers)
        {
            if (numbers.Count == 0)
            {
                return 0;
            }
            return numbers.Sum() / (double) numbers.Count;
        }
        

This is a way that ChatGPT can help you to improve the efficiency and readability of your code.

Writing test cases

ChatGPT can be used to generate test cases for C# code. For example, suppose you want to generate a test case for a C# method that calculates the average of a list of integers. You could prompt ChatGPT with the following:

    Write test cases for the following C# method that cover edge cases:

        public int Factorial(int n)
        {
            if (n < 0)
            {
                throw new ArgumentException("n must be non-negative");
            }
            if (n == 0)
            {
                return 1;
            }
            return n * Factorial(n - 1);
        }
        

This is what Chat GPT returns:

[Test]
public void TestFactorialEdgeCases()
{
    Assert.Throws<ArgumentException>(() => Factorial(-1));
    Assert.AreEqual(1, Factorial(0));
    Assert.AreEqual(1, Factorial(1));
    Assert.AreEqual(2, Factorial(2));
    Assert.AreEqual(6, Factorial(3));
    Assert.AreEqual(40320, Factorial(8));
}


Writing documentation

ChatGPT can be used to generate documentation for existing C# code by prompting it with descriptions of the code and its intended behavior.

    Write documentation for the following C# method:

        public double Average(List<int> numbers)
        {
            if (numbers.Count == 0)
            {
                return 0;
            }
            return numbers.Sum() / (double) numbers.Count;
        }

        The method calculates the average of the integers in the input list. If the list is empty, it returns 0.
        

This is what Chat GPT returns:

    /// <summary>
        /// Calculates the average of the integers in the input list.
        /// If the list is empty, it returns 0.
        /// </summary>
        /// <param name="numbers">The list of integers to calculate the average of.</param>
        /// <returns>The average of the integers in the input list, or 0 if the list is empty.</returns>
        public double Average(List<int> numbers)
        {
            if (numbers.Count == 0)
            {
                return 0;
            }
            return numbers.Sum() / (double) numbers.Count;
        }
        

This documentation uses C#'s XML documentation comments to provide a summary of the method's behavior and describe its input and output parameters.

Scaffolding code

Scaffolding is the process of generating basic code structures or templates that can be customized and built upon by a developer. ChatGPT can be used for scaffolding code to speed up development by generating code snippets or templates based on a developer's prompts.

For example, suppose you are building a C# application that needs to make HTTP requests to a REST API. You could use ChatGPT to generate the basic structure of a class that handles the HTTP requests, as well as methods for making common types of requests such as GET, POST, PUT, and DELETE.

You give the prompt: "Write a C# class that can be used to make HTTP requests to a REST API. The class should include methods for making GET and POST requests." and get the following output:

    public class ApiClient
    {
        private readonly HttpClient _httpClient;

        public ApiClient(string baseUrl)
        {
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri(baseUrl)
            };
        }

        public async Task<T> GetAsync<T>(string url)
        {
            var response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsAsync<T>();
        }

        public async Task<HttpResponseMessage> PostAsync<T>(string url, T data)
        {
            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
            return await _httpClient.PostAsync(url, content);
        }
    }
    

The generated code is concise and easy to understand, and it would likely take a C# developer much longer to write it manually. By no means is the code done, but it is ready to start refactoring and customizing to fit your needs. This can save you hours of work every day, and it can help you stay competitive with other developers who are using AI effectively to improve their efficiency.

Here is another prompt to ChatGPT:

"as a c# developer write a method to make 1000 REST calls but no more than 10 concurrent requests at a time. Use SemaphoreSlim to limit the number of concurrent requests. Use Task.WhenAll to log a message when all calls are complete" and the output is:

|
|
| public async Task Make1000RestCallsAsync()
| {
| // Create a SemaphoreSlim with a maximum of 10 concurrent requests
| SemaphoreSlim semaphore = new SemaphoreSlim(10);
|
| // Create a list of tasks to make the 1000 REST calls
| List
task  tasks = new List
  task
    | ();
    | for (int i = 0; i &lt; 1000; i++)
    | {
    | // Acquire the semaphore before making the request
    | await semaphore.WaitAsync();
    |
    | // Create a task to make the request
    | tasks.Add(Task.Run(async () =&gt;
    | {
    | try
    | {
    | // Make the REST call
    | await MakeRestCallAsync();
    | }
    | finally
    | {
    | // Release the semaphore
    | semaphore.Release();
    | }
    | }));
    | }
    |
    | // Wait for all tasks to complete
    | await Task.WhenAll(tasks);
    |
    | // Log a message when all calls are complete
    | Console.WriteLine(&quot;All calls complete&quot;);
    | }