Extracting information about key pressed in .NET console applications

Console applications let you extract the key(s) pressed by the user using the Console.ReadKey() method. It returns an object of type ConsoleKeyInfo which includes a number of useful properties.

Read more of this post

How to redirect standard error output for a .NET console application

Normally a .NET console application writes its exception messages directly to the console so that the user can view them.

Here’s an example:

static void Main(string[] args)
{
	RunStandardErrorRedirectExample();			
	Console.ReadKey();
}

private static void RunStandardErrorRedirectExample()
{
	try
	{
		double res = Divide(100, 0);
		Console.WriteLine(res);
	}
	catch (Exception ex)
	{
		using (TextWriter errorWriter = Console.Error)
		{
			errorWriter.WriteLine(ex.Message);
		}
	}
}

private static double Divide(int divideWhat, int divideBy)
{
	return divideWhat / divideBy;
}

You’ll get “Attempted to divide by 0” in the console.

However, this is not the only option you have. The standard error output channel can be overridden.

Read more of this post

Raise exception if HTTP response is other than Success

The standard way nowadays to perform a HTTP request in .NET is using the HttpClient object in the System.Net.Http namespace. There are other objects with a similar purpose, such as HttpWebRequest but HttpClient has probably become the most common.

Read more of this post

Checking whether an enum value exists by a parse test in C#

Say you have the following ShipmentOption enumeration:

public enum ShipmentOption
{
	Land,
	Sea,
	Air
}

By default each enumeration value will have an integer representation starting with 0. So 0 corresponds to Land, 1 to Sea and 2 to Air. In addition, each enumeration entry can be stringified such as “Land”, “Sea” and “Air”.

The Enum class has a TryParse method that helps you convert the string representation of the enumeration into the actual enumeration value.

Read more of this post

Introduction to generics in C# Part 6

Introduction

In the previous post we looked at constraints on generic type parameters using the ‘where’ keyword. We looked at an example where we wanted to limit the usage of an interface, IPropertiesPrinter, to those objects that implement another interface, namely IPrintable. We wanted to make sure that the generic type, i.e. the object to be printed, will have the GetProperties() method available. Without the constraint the generic object would only have the methods inherited from the Object class, such as ToString and GetHashCode.

In this post we’ll look at an example where not even constraints seem to provide a solution. I wanted to include this example in this series to show that sometimes not even generics can help create generic code.

Read more of this post

Introduction to generics in C# Part 5

Introduction

In the previous post we looked at how to declare multiple type parameters. We saw that it was a very simple thing to do. We just add as many parameters as we need and separate them by a comma. It’s perfectly fine to refer to those parameters later on in the class level functions. It’s equally fine to put multiple parameters on the function level as well if you don’t want to make the entire class generic.

In this post we’ll take a quick look at parameter type constraints.

Read more of this post

Introduction to generics in C# Part 4

Introduction

In the previous post we saw how to build generic functions. It’s not necessary to make an entire class generic if we want to keep the functions independent of any class level type parameters. We saw an example, a configuration settings reader, where it was unnecessary to declare the return type on the class level. That would put a constraint on the usage of a single configuration reader instance. Instead, we let the caller define the return type on the ReadConfigurationValue function which makes the configuration reader class more flexible.

In this post we’ll look at declaring multiple generic parameters.

Read more of this post

Introduction to generics in C# Part 3

Introduction

In the previous post on generics we looked at an example of reading application settings from a configuration file. Application settings come in key-value pairs where the value can be of different type: string, integer, boolean and various others. The generic ConfigurationReaderService helped us improve the non-generic code where the function caller had to take care of the type conversion and validation steps.

In this post we’ll further improve our code by looking into generic methods.

Read more of this post

Introduction to generics in C# Part 2

Introduction

In the previous post we started looking into generics in .NET. We also saw an initial example where an abstract base class had a type parameter to define the type of the Id property. The implementing classes could then all derive from this generic class and define their Id type along the way.

In this post we’ll look at another example where generics can be useful.

Read more of this post

Introduction to generics in C# Part 1

Introduction

Generics is a way to generalise your code in .NET. We can generalise the types that an object and/or a function operates on. With generics we can reuse the same portion of code with multiple types. If used in appropriate places it can be a great tool against code duplication.

Generics is nothing new, it was a new language feature in .NET 2.0.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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