TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Asynchronous Methods

C# Asynchronous Methods for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Asynchronous Method

C# asynchronous method is a special method that executes asynchronously. C# provides async modifier to make a method asynchronous. It is used to perform asynchronous tasks.

C# await expression is used to suspend the execution of a method.

If a method which uses async modifier does not contain await expression, executes synchronously.

Note: the async method cannot use ref or out parameters.


C# Asynchronous Method Return Types

An async method can use any one of the following return type.

  • Task
  • Task<TResult>
  • Void (for event handlers)
  • System.Threading.Tasks.ValueTask<TResult>

We should add asyncsuffix to the method name because of naming convention. Following is a typical syntax to define asynchronous method.

Syntax

public async Task<int> ExampleMethodAsync()  
{  
    // statements  
}

We can use System.Net.HttpClient, Microsoft.Azure.EventHub.Core libraries that contain asynchronous operations.

In the following example, we are using using System.Net.Http; namespace to execute an asynchronous task.

This namespace is not available by default, so we need to install it by using the package manager console.

To open console, follow the instruction as we did in the following screenshot.

CSharp Asynchronous Methods 1

This will open a console window, where we can pass namespace name to install it in our project. Write the following command, as we did in the following screenshot.

PM> Install-Package System.Net.Http
CSharp Asynchronous Methods 2

After installing it, now we can execute the application.

C# Asynchronous Method Example

using System;
using System.Threading.Tasks;
using System.Net.Http;
namespace CSharpFeatures
{
    class AsynchronousMethod
    {
        static void Main(string[] args)
        {
           Task result = add();
           Console.WriteLine("length: {0}", result.Result);
        }
// Asynchronous method
        async static Task add()
        {
            Task TaskUrl = new HttpClient().GetStringAsync("http://www.TheDeveloperBlog.com");
            string result = await TaskUrl;
            return result.Length;
        }
    }
}

Output

length: 36006





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf