Return output slowly in C#

HomeC#

Return output slowly in C#

How to escape braces in a format string in C#
Make text be “typed out” effect in C#
Difference between String (Uppercase s) and string (lowercase s)

There are occasions when we want to give some type of effect to the output we’re returning, for example in a game or in the console to make it look like if was a typewriter, to accomplish this is simple with the following code

Create a helper method

using System.Threading.Tasks;
public static async Task ReturnTextSlowly(string word, TimeSpan delay = default) 
{       
    if(delay == default) delay = TimeSpan.FromSeconds(1);

    Console.Write(word);
    await Task.Delay(delay);
}

and then you can call it like this

public class Program {
    public async Task Main() {
        while(true) 
        {
            await ReturnTextSlowly("slowtext");
        }
    }
}

COMMENTS

DISQUS: 0