Async Main methods in C# 7.1

Async methods have the tendency to bubble up to the top of the call chain. Asynchronous repository calls in a large web application are often accompanied by asynchronous methods in the controllers as well.

However, this has until recently posed a problem in Console applications where the “top” function is Main. The Main function, as we all know, must adhere to a small number of rules, one of which being that it must either be void or return an integer in case we care about the exit code. So what could we do if we wanted to call upon an asynchronous method from Main? We couldn’t change its signature unfortunately to make it async so we turned to other ways like calling GetAwaiter().GetResult() on the awaitable method.

C# 7.1 solves this problem by enabling another type of Main method signature:

Read more of this post


Using the ValueTask of T object in C# 7.0

By now probably all .NET developers are aware of the await and async keywords, what they do and how they work.

Here’s a small example where the CalculateSum function simulates a potentially time-consuming mathematical operation:

public class AsyncValueTaskDemo
	public void RunDemo()
		int res = CalculateSum(0, 0).Result;

	private async Task<int> CalculateSum(int a, int b)
		if (a == 0 && b == 0)
			return 0;

		return await Task.Run(() => a + b);

Read more of this post

Throwing exceptions in expressions in C# 7.0

C# 7.0 makes it possible to throw exceptions with ternary and null-coalescing operators.

Here’s an example where we throw an exception if the divisor is 0:

private double Divide(double what, double withWhat)
	return withWhat != 0 ? what / withWhat : throw new ArgumentException("nono");

Read more of this post

Performing some action while waiting for a key to be pressed in .NET console applications

You can wait for the user to press some button in a .NET console application using the Console.ReadKey() method. That’s simple and easy to use, but occasionally you might want to perform some action while waiting for the user to press a key.

The KeyAvailable property of the Console object helps you achieve just that.

Read more of this post

How to hide the text entered in a .NET console application

You’ve probably encountered console applications that ask for a password. It’s very likely that the password will stay hidden otherwise other people viewing your screen can easily read it.

This short post will present a possible solution on how to achieve a hidden string input in a .NET console application.

Read more of this post

Implementing an enumerator for a custom object in .NET C#

You can create an enumerator for a custom type by implementing the generic IEnumerable of T interface. Normally you’d do that if you want to create a custom collection that others will be able to iterate over using foreach. However, there’s nothing stopping you from adding an enumerator to any custom type if you feel like it, it’s really simple.

Consider the following Guest class:

public class Guest
	public string Name { get; set; }
	public int Age { get; set; }

Guests can be invited to a Party:

Read more of this post

Using the KeyedCollection object in C# .NET

The abstract generic KeyedCollection object can be used to declare which field of your custom object to use as a key in a Dictionary. It provides sort of a short-cut where you’d want to organise your objects in a Dictionary by an attribute of that object.

Let’s take the following object as an example:

public class CloudServer
	public string CloudProvider { get; set; }
	public string ImageId { get; set; }
	public string Size { get; set; }

The Image IDs are always unique so the ImageId property seems to be a good candidate for a dictionary key.

Here’s an example:

Read more of this post


A great site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: