N
N
Nikita072020-09-24 10:22:06
C++ / C#
Nikita07, 2020-09-24 10:22:06

How to display the name of the countries through the API?

The essence of the task is as follows: It is
necessary to create an application in which the user, by entering the name of the country, will receive various information about it (Capital, Area, etc.)
I found RESTCountries.NET on the Internet, but when I request to display all countries, nothing happens, which I am I not doing that? Please help me understand
Here is an example code:

using RESTCountries.Models;
using RESTCountries.Services;
    class Program
    {
        static void Main(string[] args)
        {
            Test();
        }
        static async void Test()
        {
            List<Country> countries = await RESTCountriesAPI.GetAllCountriesAsync();
            Console.WriteLine(countries.Count);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Voland69, 2020-09-24
@Voland69

To begin, await Test(); in the Main() method - otherwise the program ends without waiting for the completion of the asynchronous operation.
And the signature of Main in this case will be the following - static async Task Main(string[] args).

Y
yuopi, 2020-09-24
@yuopi

You need to learn about asynchrony.
How returning void differs from Task in async methods

static void Main(string[] args)
        {
           Task.WaitAll(Test());
        }
        static async Task Test()
        {
            List<Country> countries = await RESTCountriesAPI.GetAllCountriesAsync();
            Console.WriteLine(countries.Count);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question