How to hide the key pressed in a .NET console application

You probably know how to read the key pressed by the user in a .NET console application by the Console.ReadKey() method:

ConsoleKeyInfo pressedKey = Console.ReadKey();
Console.WriteLine("You have pressed: {0}", pressedKey.Key);

Read more of this post

Implementing an indexer for your object with C# .NET

Indexers are used extensively when accessing items in an array or List:

Friend f = friends[2];

It’s fairly easy to implement your own indexer. Imagine a table with guests sitting around. We could implement an indexer to easily access guest #n.

The Guest object is simple with only one property:

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

Read more of this post

Set various options for your HTTP calls through the HttpClientHandler object in .NET

The HttpClient object in the System.Net.Http namespace has become quite a standard object in .NET to initiate HTTP calls in code. In this short post I’d like to draw your attention to one of its constructors which allows you to set various useful options.

Read more of this post

Introducing the Command Line Parser Library to make parsing command line arguments easier

If you’ve worked extensively with .NET console applications then you’ve probably encountered difficulties with parsing the command line arguments. They are passed into the args argument of the Main function. Then it’s your task to investigate if the caller has passed in all necessary arguments:

  • Have all the mandatory arguments been passed in?
  • What default value do we take for optional arguments?
  • What about the ordering of arguments?

…et cetera, the list could grow a lot longer.

Fortunately there are libraries that can help you parse the arguments. One such library is called Command Line Parser Library available from NuGet here. You can find its project page here with some initial code examples.

Read more of this post

A basic example of using the ExpandoObject for dynamic types in .NET C#

The ExpandoObject in the System.Dynamic namespace is an interesting option if you ever need to write dynamic objects in C#. Dynamic objects are ones that are not statically typed and whose properties and methods can be defined during runtime. I’ve come across this object while I was discovering the dynamic language runtime (DLR) features in .NET. We’ve seen an example of that in this post with the “dynamic” keyword.

Read more of this post

Divide an integer into groups with C#

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the start integer.

The following function will perform just that:

Read more of this post

Calculate the number of months between two dates with C#

Say you’d like to calculate the difference between two dates in terms of number of months.

The following simple function will do just that: return the absolute number of months between two dates:

Read more of this post

Various quarter-related DateTime functions in C#

The DateTime object – or struct – lacks functions for quarters. You can e.g. add minutes, months, days etc. to a date but not quarters.

Here comes a short list of simple quarter-related functions.

Read more of this post

How to change the size of the command prompt in a .NET console application

Occasionally you might need to change the size of the console window in a .NET console application. There are methods and properties available in the Console object that enable you to perform this operation easily.

Read more of this post

Truncate a DateTime in C#

Occasionally you need to truncate a date to the nearest year, month, day etc. E.g. you need to transform the date 2015-06-05 15:33:30 into 2015-06-05 00:00:00, i.e. truncate it to the nearest day and set the lower levels of the date to 0.

Here comes a series of extension methods to help you with that:

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.