Skip to main content

Improved C# Code Samples

· 5 min read

In this release, we have improved the code quality of our generated C# code samples by making them idiomatic.

Details

This release introduces significant improvements to the code samples provided for your API. We have made several updates to the code samples to ensure that they are up-to-date, easier to understand, and more user-friendly. The new code samples are designed to help developers get started with their API quickly and easily, without having to spend a lot of time reading documentation or trying to figure out how to use the API.

What Has Changed?

The code samples are refactored and reformatted to improve the code quality and make them style-compliant using the Dotnet code analyzer. The used ".editorconfig" is publicly available here. Other changes made to the generated code samples are listed below.

  • Added in-line parameter initialization.
  • Added support for multidimensional array.
  • Added comments and explanations to the code samples to provide more context and help developers understand how to use the API.
  • Fixed bugs and errors in the code samples to ensure that they work as expected.

Imports Section Improvements

Imports have been sorted in alphabetical order and empty lines have been removed to fix identified lint issues.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Tester.Standard.Controllers;
using Tester.Standard.Exceptions;
using Tester.Standard.Models;
using Tester.Standard.Utilities;

Parameters Initialization Section Improvements

The explicit type parameter declaration and in-line initialization have been added to ensure the code samples are easier to understand and more user-friendly.

Model Initialization

Employee employeeModel = new Employee
{
Name = "John Doe",
Salary = 4200,
Boss = new Person
{
Name = "Jane Doe",
Age = 45,
},
};

Array Initialization

List<int> numberArray = new List<int>
{
210,
211,
212,
}

Map Initialization

Dictionary<string, int> numberMap = new Dictionary<string, int>
{
["key0"] = 110,
["key1"] = 111,
["key2"] = 112,
};

Multidimensional Array Initialization

The missing support multidimensional array of primitive and non-primitive types in C# code samples has been added.

List<List<int>> numbers2dArray = new List<List<int>>
{
new List<int>
{
158,
159,
},
new List<int>
{
256,
257,
},
};

API Call Section Improvements

The code block is reformatted and comments are added to provide more context and help developers understand how to use the API.

try
{
List<ServerResponse> result = await responseTypesController.SendAsync(model);
}
catch (ApiException e)
{
// TODO: Handle exception here
Console.WriteLine(e.Message);
}

A Comparison of Complete Example Code Sample

The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send endpoint.

Employee Model

"employee_model": {
"name": "John Doe",
"salary": 4200,
"boss": {
"name": "Jane Doe",
"age": 45,
}
}

Generated code samples

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Tester.Standard.Controllers;
using Tester.Standard.Exceptions;
using Tester.Standard.Models;
using Tester.Standard.Utilities;

namespace Testing
{
public class Program
{
public static async Task Main()
{
TesterClient client = new TesterClient.Builder()
.Suites(SuiteCode.Hearts)
.Build();

IResponseTypesController responseTypesController = client.ResponseTypesController;

Employee employeeModel = new Employee
{
Name = "John Doe",
Salary = 4200,
Boss = new Person
{
Name = "Jane Doe",
Age = 45,
},
};

try
{
List<ServerResponse> result = await responseTypesController.SendAsync(employeeModel);
}
catch (ApiException e)
{
// TODO: Handle exception here
Console.WriteLine(e.Message);
}
}
}
}

Bugs Fixed

  • Fixed an issue with DateTime initialization for Rfc1123, Rfc3339 and Unix Timestamp formats.
  • Fixed an issue where generated client initialization code block was uncompilable in the case of Basic Authentication.
  • Fixed an issue where the HttpClientConfig block was added to generated client initialization even if it was not configured.