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

Using client certificates in .NET part 5: working with client certificates in a web project

Introduction

In the previous post we looked at a couple pf examples on how to work with digital certificates in C# code. In particular we saw how to load certificates from a certificate store, how to search for and how to validate one.

In this post we’ll go through how to attach a client certificate to a web request and how to extract it in a .NET Web API 2 project.

Read more of this post

Checking whether an enum value exists by an integer reference 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. Imagine that a method accepts the numeric value like this:

public void SendShipment(int numericShipmentType)

Then the incoming numeric parameter could be anything. How can we check whether an integer has a corresponding enumeration value?

The following code will do just that:

public void SendShipment(int numericShipmentType)
{
	if (Enum.IsDefined(typeof(ShipmentOption), numericShipmentType))
	{
		Console.WriteLine("This type is defined: {0}", (ShipmentOption)numericShipmentType);
	}
	else
	{
		throw new InvalidEnumArgumentException("ShipmentOption", numericShipmentType, typeof(ShipmentOption));
	}
}

The InvalidEnumArgumentException class is defined in the System.ComponentModel namespace.

Passing in 2 will result in the following output:

This type is defined: Air

An invalid integer argument will throw an exception:

The value of argument ‘ShipmentOption’ (5) is invalid for Enum type ‘ShipmentOption’.

View all various C# language feature related posts here.

Using client certificates in .NET part 4: working with client certificates in code

Introduction

In the previous post we discussed how to install certificates into the certificate store. We looked at the tool mmc.exe and its certificate handler snap-in. We also inspected the imported certificates visually and verified that the client certificate is valid.

In this post we’ll see a couple of examples how to work with client certificates in code.

Read more of this post

How to redirect standard input for a .NET console application

Normally .NET console applications read their input from the console through Console.ReadLine(). The user is prompted for some input, they enter some text, press enter and the console can read this input.

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

Read more of this post

Packing and unpacking files using Tar archives in .NET

You must have come across files that were archives using the tar file format. Tar files are most often used on Unix systems like Linux but it happens that you need to deal with them in a .NET project.

You can find examples of .tar files throughout the Apache download pages, such this one. You’ll notice that .tar files are often also compressed using the GZip compression algorithm which together give the “.tar.gz” extension: they are files that were packed into a tar archive and then zipped using GZip. You can find an example of using GZip in .NET on this blog here. I have only little experience with Linux but I haven’t seen standalone “.tar” files yet, only ones that were compressed in some way. This is also the approach we’ll take in the example: pack and compress a group of files.

Read more of this post

Using client certificates in .NET part 3: installing the client certificate

Introduction

In the previous post we looked at two tools that help you create self-signed certificates: makecert.exe and pvk2pfx. Makecert performs the bulk of the certificate creation process and pvk2pfx is a packaging tool to build pfx files. Pfx files are easy to import into the certificate store. We also created a root certificate and derived a client certificated from that root. We therefore applied the idea of the chain of trust: if we trust the root then we also trust all its derived certificates.

In this post we’ll import the client certificate into the certificate store.

This process is very similar to importing the server side (SSL) certificate. We saw that process in this post.

certmgr and MMC snap in

The c:\windows\system32 folder includes two GUI tools for certificate management: certmgr.msc and mmc.exe. MMC.exe is a more general tool where you can import so-called snap-ins. Certificates have their own snap-in.

Run mmc.exe as an administrator. The following empty window will open:

Read more of this post

Python language basics 76: class property setters

Introduction

In the previous post we looked at class level methods. We saw that they weren’t much different from “normal” methods. The fact that the first input parameter is “self” is what differentiates class level methods. Also, they can be called on an instance of a class, i.e. an instantiated object. The extra properties that the method needs are provided after “self”.

In this post we’ll look at a special group of class methods: setters.

Read more of this post

Python language basics 75: class level methods

Introduction

In the previous post we looked at the basics of validation. In particular we looked at an example where the values sent to the initialiser were validated in code. Validation is important in order to stop invalid values being assigned to class level variables. However, validation can occur just about anywhere in code. E.g. if a numeric input is required from the user then you’d almost certainly need to validate it so that the code doesn’t stop with an exception when trying to convert the string input into a number.

In this post we’ll quickly look at class level methods.

Read more of this post

Formatting positive and negative numbers with C# .NET

Let’s say you have the following three numbers:

  • 13.45
  • -32.56
  • 0

…and you’d like to format them as follows:

  • +13.45
  • -32.56
  • (0)

There’s a special format string that can be used for this purpose. It consists of 3 parts separated by a semi-colon. The first part applies to positive numbers, the second to negative numbers and the third to zeroes:

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.