posts - 19 , comments - 8 , trackbacks - 0

C#

Wpf TreeView MVVM view model

You can find my implementation of an MVVM TreeView model for WPF on github.

Link: https://github.com/rob-blackbourn/JetBlack.WpfTreeView.

Posted On Tuesday, May 19, 2015 9:34 AM | Comments (0) | Filed Under [ C# .Net MVVM TreeView WPF ]

Indexing a one dimensional array by n dimensions

There's an example on how to index a 1-dimensional array by n-dimensions on github.

Link: https://github.com/rob-blackbourn/JetBlack.ArrayIndexing.

Posted On Tuesday, May 19, 2015 9:31 AM | Comments (0) | Filed Under [ C# .Net Array Indexing ]

Network programming with reactive extensions

I've written some examples of using reactive extensions for network communication. You can find the project on github.

Link: https://github.com/rob-blackbourn/JetBlack.Network.

Posted On Tuesday, May 19, 2015 9:28 AM | Comments (0) | Filed Under [ C# Reactive Extensions rx .Net Network Socket Select TcpClient TcpListener ]

Monads in C#

I've written some example moands with practical examples. Checkout the github project.

Link: https://github.com/rob-blackbourn/JetBlack.Monads.

Posted On Tuesday, May 19, 2015 9:25 AM | Comments (0) | Filed Under [ C# .Net Monads ]

A timeout dictionary in C#
Overview My use case was a authentication system which stored user details. Fetching this information was expensive, so it was decided to only do this if the record was older than 15 minutes. The following code implements a dictionary were the values "timeout" after a given time period. The timeout dictionary The following code implements the dictionary. The constructor takes a date time provider which you can find in a previous post. This allows me to pass in a faster implementation of DateTime, ......

Posted On Friday, December 19, 2014 9:17 AM | Comments (1) | Filed Under [ C# TimeoutDictionary ]

Testing code with time dependency in C#
Introduction Testing code that is date or time dependent is clearly problematic! The first step is to create an interface which we will use instead of accessing the DateTime object itself. The interface using System; namespace JetBlack.Common.Timers { public interface IDateTimeProvider { DateTime Today { get; } DateTime Now { get; } long Ticks { get; } } } A native date time provider We can wrap up the standard date time object with the following code. using System; namespace JetBlack.Common.Timers ......

Posted On Thursday, December 18, 2014 4:55 PM | Comments (0) | Filed Under [ C# DateTime Testing ]

Array pooling in C#
Introduction The following class reduces garbage collection by maintaining a pool of allocated arrays. DisposableValue I wanted a simple way to manage the lifetime of the arrays. To do this I have borrowed an idea from reactive extensions using System; namespace JetBlack.Common { public class DisposableValue<T> : IDisposable { private readonly Action _dispose; public DisposableValue(T value, Action dispose) { _dispose = dispose; Value = value; } public T Value { get; private set; } public void ......

Posted On Thursday, December 18, 2014 3:57 PM | Comments (1) | Filed Under [ C# array ArrayPool ]

A Generic Assert Class for C#
Overview The following code provides a mechanism for creating asserts which throw a generic exception. My initial motivation was to express in a more concise way common argument and result checking code. For example the following code: public string Read(Stream stream, Encoding encoding, int count) { if (stream == null) throw new ArgumentNullException("stre... "The stream must be created."); if (encoding == null) throw new ArgumentNullException("enco... "The encoding must be specified."); if ......

Posted On Thursday, December 4, 2014 1:25 PM | Comments (0) | Filed Under [ C# Assert Generic ]

A Rational Number Class in C#
Overview The following code implements a rational number. The code I have split the code into partial classes to keep the functionality distinct.For the underlying types I have decided to use longs throughout as the main problem with rational numbers is overflow. Furthermore I have chosen to always reduce the fraction. This adds an overhead on each computation, but contains overflow problems. AlgorithmsThe first task is to write the greatest common divisor function. From my previous post I have established ......

Posted On Friday, November 14, 2014 11:02 AM | Comments (2) | Filed Under [ C# rational fraction ]

Evaluating the Greatest Common Divisor function in C#
Available Routines There are a number of basic routines. Two that are attributable to Euclid are the subtraction and modulus method. There is another routine be Stein which uses binary arithmatic. Lastly there is the "trial and error" version which is useful as a check on validity. The algorithms The algorithms are provided in the code below. using System; namespace JetBlack.Common { public static class GreatestCommonDivisor { public enum Algorithm { TrialAndError, EuclidModulus, EuclidModulusShort, ......

Posted On Friday, November 14, 2014 10:56 AM | Comments (0) | Filed Under [ C# GCD greatest common divisor ]

Creating an observable on a task with multiple subscribers
The following example creates an observable which wraps a task with multiple subscribers.Note that the task is cancelled when the last subscriber is disposed. using System; using System.Reactive.Linq; using System.Threading.Tasks; namespace JetBlack.Reactive.TaskExamples { class MultipleSubscribers { public void Test() { var observable = Observable.Create<int>( (observer, token) => Task.Factory.StartNew(() => { var i = 0; while (!(token.WaitHandle.WaitOne... || token.IsCancellationRequest... ......

Posted On Thursday, October 16, 2014 10:12 AM | Comments (0) | Filed Under [ C# Reactive Extensions rx Observable ]

Creating an observable on a task with cancellation logic
The following code demonstrates how to create an observable which runs a task. When the subscription is disposed the task completes. using System; using System.Reactive.Linq; using System.Threading.Tasks; namespace JetBlack.Reactive.TaskExamples { public class SingleSubscriber { public void Test() { var observable = Observable.Create<int>( (observer, token) => Task.Factory.StartNew(() => { var i = 0; while (!(token.WaitHandle.WaitOne... || token.IsCancellationRequest... observer.OnNext(i++); ......

Posted On Thursday, October 16, 2014 9:59 AM | Comments (0) | Filed Under [ C# Reactive Extensions rx Observable ]

Caching Dictionary
This blog describes an implementation of a local/persistent caching dictionary bringing together the classes discussed in the Heap, PersistentDictionary, and CircularBuffer blogs. Design The implementation uses an in memory dictionary and a persistent dictionary. The recently accessed items remain in the local dictionary, while the less used are moved to the persistent store. As older values are accessed they are moved back into the local store. using System; using System.Collections; using System.Collections.Generic; ......

Posted On Monday, September 1, 2014 11:52 AM | Comments (0) | Filed Under [ C# CachingDictionary caching dictionary ]

Persistent Dictionary
This blog describes the implementation of a persistent dictionary. There are a number of excellent solutions to this online, but most were highly complex. This is a simple implementation which was sufficient for my purpose. It uses the classes described in the Heap blog. The Cache First we defined the interface for the persistent cache. This follows the traditional CRUD pattern. using System; using JetBlack.Patterns.Heaps; namespace JetBlack.Patterns.Caching { public interface ICache<T> : IDisposable ......

Posted On Monday, September 1, 2014 11:13 AM | Comments (0) | Filed Under [ C# persistent dictionary PersistentDictionary ]

Heap
This post describes a heap data structure. In this implementation a heap manages a sequential array of data which grows upwards from the bottom. Blocks of this array can be allocated, freed, read and written to through handles. The heap attempts to keep itself small by managing a list of free blocks that can be reallocated. Design The primary operations handled by the heap will be memory management: Allocate and Deallocate, and reading and writing: Read and Write. As will become clear later on it ......

Posted On Monday, September 1, 2014 9:51 AM | Comments (0) | Filed Under [ C# Heap stream heap file stream heap StreamHeap FileStreamHeap ]

Circular Buffer in C#
A circular buffer is buffer of fixed length. When the buffer is full, subsequent writes wrap, overwriting previous values. It is useful when you are only interested in the most recent values. Design Goal This is a circular buffer I needed as a component for a caching layer. It is largely a blatant ripoff of the many implementations previously published on the web, with a few changes which might prove useful to those with similar objectives. My first specific requirement was to model the structure ......

Posted On Monday, September 1, 2014 9:04 AM | Comments (3) | Filed Under [ C# circular buffer ]

Powered by: