In C#, does it make sense to write code like:
await Console.In.ReadLineAsync()
?
because the application has to wait for user's response to decide what to do next anyway.
so I might as well right something like:
Console.In.ReadLine();
In C#, does it make sense to write code like:
await Console.In.ReadLineAsync()
?
because the application has to wait for user's response to decide what to do next anyway.
so I might as well right something like:
Console.In.ReadLine();
It is similar if you want use them in a single task console application. Everything changes when you want to have a multitasking console application and have to run parallel tasks with ongoing one.
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static async Task Main()
{
await ReadCharacters();
}
static async Task ReadCharacters()
{
String result;
using (StreamReader reader = File.OpenText("existingfile.txt"))
{
Console.WriteLine("Opened file.");
result = await reader.ReadLineAsync();
Console.WriteLine("First line contains: " + result);
}
}
}
}
By your request I Changed this code to bellow
class Program
{
static async Task Main()
{
Task thisTask= Task.Run(()=>ReadCharacters());
int res = LogicalOperation(5, 6);
await thisTask;
Console.ReadLine();
}
static async Task ReadCharacters()
{
String result;
using (StreamReader reader = File.OpenText("existingfile.txt"))
{
Console.WriteLine("Opened file.");
result = await reader.ReadLineAsync();
Console.WriteLine("First line contains: " + result);
}
}
static int LogicalOperation(int x,int y )
{
Console.WriteLine("result of logial Operation is:" + (x*y).ToString());
return x*y;
}
}