Access modifiers in C# 7.2

C# 7.2 comes with a new access modifier “private protected”. It sounds like a weird combination so let’s see how the various access modifiers in the current release of C# work.

Let’s start with an abstract class called Animal:

public abstract class Animal
{
	private String privateName = "Private name";
	protected String protectedName = "Protected name";
	protected internal String protectedInternalName = "Protected internal name"; 
	private protected String privateProtectedName = "Private protected name";
	public String publicName = "Public name";
}

Read more of this post

Advertisements

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;
		Console.WriteLine(res);
	}

	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

Enabling C# 7.1 and 7.2 features in your .NET code

C# 7.0 has been quickly enhanced with a couple of new features in 7.1 and 7.2. If you start Visual Studio 2017 and try e.g. the new “private protected” access modifier then the compiler may not accept it:

public abstract class Animal
{
	private String privateName = "Private name";
	protected String protectedName = "Protected name";
	protected internal String protectedInternalName = "Protected internal name"; 
	private protected String privateProtectedName = "Private protected name";
	public String publicName = "Public name";
}

If it puts a red line under “private protected” saying that this access modifier combination is not allowed then you’ll know that C# 7.2 has not been enabled yet.

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

Efficient linked lists in .NET

Sometimes you need a collection that’s modified a lot: you insert, update and remove items. A List of T is then inefficient as it needs to constantly rearrange all other items. The LinkedList class might be a better candidate.

A linked list is a doubly-linked list. Each item has a Next and Previous pointer to look at which element comes right before and after a particular object in the list. A linked list is very efficient at inserting and deleting items in particular.

Initialisation:

Read more of this post

Concatenate strings with the StringJoiner class in Java 8

Java 8 introduces a new object which enables you to join individual strings: the StringJoiner.

The StringJoiner has two overloads. The simpler one accepts a delimiter:

StringJoiner sj = new StringJoiner(" | ");
sj.add("Hello").add("my").add("dear").add("world!");

System.out.println(sj.toString());

Read more of this post

ultimatemindsettoday

A great WordPress.com 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

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

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: