Concatenate two IEnumerable sequences in C# .NET

We can use the Range method to build two integer sequences as follows:

IEnumerable<int> firstRange = Enumerable.Range(10, 10);
IEnumerable<int> secondRange = Enumerable.Range(5, 20);

You can join the two sequences using the Concat method:

List<int> concatenated = firstRange.Concat(secondRange).ToList();
concatenated.ForEach(i => Debug.WriteLine(i));

You’ll see that the concatenated sequence holds all numbers from the first and the second sequence:

Read more of this post

Using the Redis NoSql database with .NET Part 1: introduction and setup

Introduction

Traditional relational databases have received a couple of strong competitors in recent years. According to the db-engines website the 10 most widely used data stores are still dominated by relational databases and it will probably be the case for long years to come. However, there are many alternatives out there that can take their place in an application as its backing store. They can either act as the main data store of the application, i.e. where all the records are persisted for later retrieval. Alternatively they serve a different but very specific storage role that is not well suited for relational databases. In other words the application is backed up by e.g. SQL Server or MongoDb as its main data storage but delegates other storage needs, such as caching to say Memcached.

Read more of this post

Namespaces in F#

The main purpose of namespaces in F# is to group related types in one container and avoid name clashes of types. Namespaces are declared using the “namespace” keyword like in many other popular languages. A significant difference between C#/Java and F# is that F# namespaces can only contain type declarations. They cannot hold any values, i.e. anything that’s declared using the “let” keyword. They are only containers for types, i.e. elements that are declared using the “type” keyword.

A single F# source file (.fs) can contain multiple namespaces. A namespace starts with the namespace declaration and ends either at the end of the source file or at the next namespace declaration. Here’s an example with two namespaces where each namespace has a single type:

Read more of this post

Discriminated unions and pattern matching in F#

Discriminated unions are similar to enumerations in F#. However, they offer more flexibility. A discriminated union is a set of types where each type can be different whereas each element of an enumeration must be an integer.

Here’s how we can declare a union of months:

type Months = January | February | March | April | May | June

We can then construct a couple of months as follows:

let jan = January
let feb = February
let apr = April
let jun = June

So it’s enough to use the name of the element in the union, we didn’t have to write the fully qualified name “Months.January” like in the case of an enumeration.

Read more of this post

Finding the user’s current region using RegionInfo in .NET C#

The CultureInfo object helps a lot in finding information about the user’s current culture. However, on occasion it may not be enough and you need to find out more about that user’s regional characteristics. You can easily retrieve a RegionInfo object from CultureInfo which will hold information about a particular country or region.

You can find the current region in two ways from CultureInfo:

Read more of this post

Introduction to ASP.NET Core part 28: summary

Introduction

In the previous post we looked at how to log out from the model .NET Core web application. We also saw how to add a simple section with the user’s login state and the appropriate login/logout/register buttons. This was also the last post in this introductory series.

In this post we’ll just summarise what we’ve learnt.

Read more of this post

Reading the value of a performance counter on Windows with C# .NET

In this post we saw how to list all performance categories and the performance counters within each category. It’s equally straightforward to read the value of a performance counter. You’ll need at least the category and the name of the performance counter. If the counter is available in multiple instances then you’ll need to specify the instance name as well.

The following code will read the CPU usage and memory usage counters:

private static void ReadValuesOfPerformanceCounters()
{
	PerformanceCounter processorTimeCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
	PerformanceCounter memoryUsage = new PerformanceCounter("Memory", "Available MBytes");			
	Console.WriteLine("CPU usage counter: ");
	Console.WriteLine("Category: {0}", processorTimeCounter.CategoryName);
	Console.WriteLine("Instance: {0}", processorTimeCounter.InstanceName);
	Console.WriteLine("Counter name: {0}", processorTimeCounter.CounterName);
	Console.WriteLine("Help text: {0}", processorTimeCounter.CounterHelp);
	Console.WriteLine("------------------------------");
	Console.WriteLine("Memory usage counter: ");
	Console.WriteLine("Category: {0}", memoryUsage.CategoryName);
	Console.WriteLine("Counter name: {0}", memoryUsage.CounterName);
	Console.WriteLine("Help text: {0}", memoryUsage.CounterHelp);
	Console.WriteLine("------------------------------");
	while (true)
	{
		Console.WriteLine("CPU value: {0}", processorTimeCounter.NextValue());
		Console.WriteLine("Memory value: {0}", memoryUsage.NextValue());
		Thread.Sleep(2000);
        }
}

Here’s an excerpt of the output:

Reading values of performance counters

You can view all posts related to Diagnostics here.

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

Introduction to ASP.NET Core part 27: showing the user status and logging out

Introduction

In the previous post we looked at the basics of authorisation and the log in process in .NET Core. The Authorization attribute which can be used for controllers and action methods helps us introduce a basic form of restrictions. The visitor must be logged in in order to reach the restricted sections in the application. The login process is handled by the built-in SignInManager service class. It offers a range of functions related to the user’s state. We also saw the importance of the return URL property which enables us to redirect the user to the page that they wanted to visit before logging in.

In this post we’ll look at how to log out and how to show the user’s status in the top section of each page.

Read more of this post

Declaring a type as a Tuple in F#

We saw how Tuples work in F# in this post. Tuples are containers for various elements such as this one:

let myFirstTuple = (45, "hello world", 5., true, addThreeNumbers)

If you’d like to declare a type to be of Tuple then you can use the ‘*’ character as follows:

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.