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

Overriding the + and – operators in C# .NET

It’s easy to override the mathemtical operators like + or – in C# to build custom operations.

Consider the following simple Rectangle class:

public class Rectangle
{
	public Rectangle(int width, int height)
	{
		Height = height;
		Width = width;
	}

	public int Width
	{
		get;
		set;
	}

	public int Height
	{
		get;
		set;
	}
}

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

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

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

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

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: