Collect day and month names of a culture in C#

The CultureInfo class helps you get the names of days and months in various languages that belong to the selected culture.

Here come 2 simple functions to collect the day and month names in string lists:

Read more of this post

Introduction to ASP.NET Core part 4: middleware and the component pipeline

Introduction

In the previous post we saw how to add a configuration file to an ASP.NET Core application and how to extract the various settings from it. We now know that the traditional web.config file has been replaced by appsettings.json and JSON is used in favour of XML. We also demonstrated that retrieving the settings values is achieved in a very different way compared to how it was done before. The ConfigurationBuilder object with its various extension methods help us load and read the configuration settings. In addition, various techniques such as using the generic IOptions of T interface let us process the different sections in appsettings.json so that we only distribute those settings that are relevant to the consuming classes that they really need. This is in contrast to passing the full list of configuration key-value pairs everywhere. The consuming class can then read the settings in an object oriented way from specialised options objects.

In this post we’ll start looking at middleware and the component pipeline in .NET Core. We’ll also write a simple custom middleware.

Read more of this post

Anonymous functions in F#

Anonymous functions in F# are used to build functions that are meant to be used within a statement. They have no names so other parts of the application can’t call them.

The following named function…:

let increment x = x + 1

…has the following anonymous counterpart:

Read more of this post

Basic function declaration in F#

We declare functions in F# using the “let” keyword. F# functions are quite spartan compared to their C# and Java counterparts. There are no parentheses, curly braces, commas, semicolons. Type declaration is optional and there’s no need for the “return” keyword if the function returns something. F# functions are similar to LINQ statements we use in C#.

The anatomy of a basic function is the following:

  • The “let” keyword
  • Followed by the function name
  • Followed by input parameters if any
  • Then comes the equality operator ‘=’ which acts as an assignment operator in this case
  • Finally we have the function body assigned to the function with the assignment operator

Read more of this post

Introduction to ASP.NET Core part 3: the configuration file

Introduction

In the previous post we looked at how dependencies and dependency injection work in .NET Core. New libraries can be downloaded to the solution in various ways: through the NuGet GUI, The NuGet package manager console, by editing project.json directly or letting Visual Studio figure out if a certain library could solve a missing reference problem. Dependency injection can be achieved through a built-in IoC – Inversion of Control container – where we register the abstractions and the corresponding dependencies in Startup.cs. We can give various lifetimes to the dependencies: transient, scoped and singleton. Then whenever a class requires a dependency the IoC container will serve up the registered concrete implementation automatically.

In this post we’ll see how to add a configuration file to the project and how to read from it.

Read more of this post

Divide an integer into groups with C# .NET

Imagine that we have an integer and we want to divide it into equal groups of another integer and put any remainder to the end of the group. E.g. if we want to divide 100 into groups/batches of at most 15 then we’ll have the following array of integers:

15, 15, 15, 15, 15, 10

The following C# function will perform exactly that function:

Read more of this post

Introduction to ASP.NET Core part 2: dependencies and dependency injection

Introduction

In the previous post we started looking at the anatomy of an ASP.NET Core application. .NET Core is a cross-platform, highly modular and lightweight application framework that is meant to be the future of application development in .NET. We identified some significant changes compared to traditional ASP.NET applications that we saw before. There are a couple of new JSON files such as global.json and project.json. Global.asax.cs is gone and has been totally replaced by Startup.cs. There’s still a web.config file but it’s not used for the same kind of configuration as before. A different json file which we haven’t seen yet will be used for that purpose. The file structure is also different in that the source files are stored in a folder called “src” by default. Note that .NET Core is still in preview mode at the time of writing this series.

In this post we’ll look at adding dependencies and using them via dependency injection and service registration.

Read more of this post

Using the let keyword in .NET LINQ to store variables within a statement

It happens that we have a LINQ statement where we want to refer to partial results by variable names while expressing some computation. The “let” keyword lets us do that. Those who are familiar for the F# language already know that “let” is an important keyword to bind some value to a variable.

Suppose we have the following list of integers:

List<int> integers = new List<int>()
{
	5, 7, 4, 6, 10, 4, 6, 4, 5, 12
};

Read more of this post

Introduction to ASP.NET Core part 1: anatomy of an empty web project

Introduction

ASP.NET Core is a brand new portable web framework by Microsoft. It is similar to the “normal” ASP.NET versions we’ve seen so far, such as ASP.NET 4.5. ASP.NET 4.5 targets the .NET 4.5 framework and conversely ASP.NET Core is primarily targeted at the .NET Core framework. However, it can also target the other .NET frameworks like .NET 4.5 or 4.6. It is currently in preview mode at the time of writing this post but developers are free to play around with it already now. It’s good to get familiar with the new features because .NET Core is meant to be the future direction of .NET development. There’s a number of significant changes compared to the earlier versions of ASP.NET that .NET developers will need to get used to.

Currently .NET Core is a subset of the full-blown .NET framework meaning that not all features of .NET are available in it. However, as the product matures the gap between .NET Core and full .NET should diminish. Eventually all new .NET projects should target .NET Core.

.NET Core marks a significant shift from Microsoft’s past policies and attitude of targeting Windows only and keeping as much as possible secret and paid. .NET Core is cross-platform and designed to run on multiple operating systems: Windows, Mac OS and Linux. It is also open-source, you can view its public GitHub repository here.

Read more of this post

How to compress and decompress files with GZip in .NET C#

You have probably seen compressed files with the “gz” extension. These are files that hold a single compressed file according to the GZIP specifications.

GZip files are represented by the GZipStream object in .NET. It’s important to note that the GZip format doesn’t support adding multiple files to the same .gz file. If you need to insert multiple files in a GZip file then you’ll need to create a “tar” file first which bundles the individual files and then compresses the tar file itself. The result will be a “.tar.gz” file. At present tar files are not supported in .NET. They are supported by the ICSharpCode SharpZipLib library available here. We’ll look at tar files in another post soon.

With that in mind let’s see how a single file can be gzipped:

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.