Using the StringComparer class for string equality with C# .NET

In this post we saw how to use the generic IEqualityComparer of T interface to indicate equality for our custom types. If you need a similar comparer for strings then there’s a ready-made static class called StringComparer which can construct string comparers for you.

The StringComparer class provides comparers for the common string comparison scenarios: ordinal, locale specific and invariant culture comparisons. This is a good MSDN article on the differences between these.

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

How to emit compiler warnings and errors in C# .NET

In this post we saw how to use the “if” preprocessor in Visual Studio to “communicate” with the compiler. Here’s a reminder of the example code which we’ll re-use here:

private static void TryPreprocessors()
{
# if DEBUG
	Console.WriteLine("You are running the Debug build");
# elif RELEASE
	Console.WriteLine("You are running the Release build");
#else
	Console.WriteLine("This is some other build.");
# endif
}

In this post we’ll look at two more preprocessor types: warning and error. If you compile a project you can get one or more errors or warnings:

Read more of this post

Build array index ranges of an integer in C# .NET

Suppose that we have a large array of data heavy objects that are impractical to handle in a single go. Instead we can read batches of the array until all elements have been processed. It can then be useful to build a range of indexes for the objects in the array that we want to extract.

E.g. if an array consists of 357 objects and we want to read at most 100 elements from it in a single batch then the size of the batches will be as follows:

100
100
100
57

…and we want to extract the elements by their indexes as follows:

0, 99
100, 199
200, 299
300, 356

…where the first number is the array start index and the second number is the array end index. Since we’re talking about array indexes the numbers are 0-based of course.

Read more of this post

Convert a dynamic type to a concrete object in .NET C#

Dynamic objects in C# let us work with objects without compile time error checking in .NET. They are instead checked during execution. As a consequence we can’t use IntelliSense while writing the code to see what properties and functions an object has.

Consider the following Dog class:

public class Dog
{
	public string Name { get; }
	public string Type { get;}
	public int Age { get; }

	public Dog(string name, string type, int age)
	{
		Name = name;
		Type = type;
		Age = age;
	}
}

Read more of this post

Flatten sequences with the C# LINQ SelectMany operator

Suppose that we have an object with a collection of other objects, like a customer with order items. Then we can also have a sequence of customers where each customer will have her own list of orders. It happens that we want to analyse all orders regardless of the customer, like how many of product A have been sold. There are several options to collect all orders from all customers and place them into one unified collection for further analysis.

The C# SelectMany operator has been specifically designed to extract collections of objects and flatten those collections into one. This post will provide a couple of examples to demonstrate its usage.

Read more of this post

Resolving null values in C#

Say you have a method which accepts a string parameter. The method may need to handle null values in some way. One strategy is to validate the parameter and throw an exception:

private static string Resolve(string input)
{
	if (input == null) throw new ArgumentNullException("Input");
.
.
.
}

Another strategy is to provide some default value with an if-else statement:

Read more of this post

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

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: