C# Regrets: Top Worst C# Features

Stumbled upon this article written by no other than Eric Lippert listing the top 10 design faults of C# language. Here is the summary,  the source to the full article is at the bottom. 

#10: The empty statement does nothing for me

Reflects on the fact that lone “;” is a legal statement

#9: Too much equality

There are too many ways check for equality: ==, Equals, ReferenceEquals, CompareTo(…).

From personal experience double.NaN == double.NaN is false but double.NaN.Equals(double.NaN) is true

#8: That operator is shifty

Weirdness around << and >> operators

#7: I’m a proud member of lambda lambda lambda

The way C# 2.0 implements anonymous delegates

#6: Bit twiddling entails parentheses

Flags Enums

#5: Type first, ask questions later

C# borrows the “type first” pattern from C and many of its other successor languages – something I got used to and the “correct” way now seems illogical to me

#4: Flag me down

The fact that you can create invalid enum values and have to manually check for this in the code

#3: I rate plus-plus a minus-minus

++i, i++, i +=1 etc. how much confusion and the pain it caused.

#2 I want to destruct finalizers

Agree with the author that finilisers in C# are symptoms of a bug. Seen it way too many times myself.

#1 You can’t put a tiger in the goldfish tank, but you can try

“array covariance” and how this could lead to run-time exceptions.

Source: http://www.informit.com/articles/article.aspx?p=2425867

Fraction Implementation in C#

I’m not really sure why Microsoft have never bothere with implementing a Fraction primitive in .NET. I’m sure there are plenty of uses as fraction allow to preserve the maximum possible precision. I have therefore decided to create my own implementation (albeit somewhat primitive at this stage) .

My implementation automatically simplifies the fraction, so if you we to create new Function(6, 3) that would be simplified to 2. The Fraction struct implements all the arithmetic operators on itself and on Int64, float, double and decimal.

Internally the Fraction is represented as two Int64: Numerator and Denominator and is always simplified upon initialisation. I initially intended to have it as an option, however following profiling the cost of simplification is not that great and the benefits outweigh the performance drawbacks. 

Fraction has explicit conversion to Int64 (although that is bound to lose precision), float, double and decimal. It supports comparison with Int64, float, double and decimal and even supports ++ and — operations.

So far I have provided more or less complete implementation with plenty of Unit Test. Now the hard word of optimising the performance begins!

Design of Fractions

Fraction is implemented as a struct (pretty obvious choice). It takes a numerator as the first argument and denominator,  it then tries to simplify the fraction using the Euclidean algorithm, so if you were to specify 333/111 it would become 3.

The implementation supports all arithmetic operations with long, float, double and decimal and can also be converted to those type by either calling the corresponding methods or using explicit cast.

You can also create a function from either a long, float, double or decimal. Conversion from a long is quite trivial however conversion from a float, double or a decimal goes through a while loop and multiplies the floating point number until it has no decimal places. This method is relatively slow and therefore is not recommended.

Apart from that the Fraction behaves like a fist class citizen: you can compare a Fraction to any other number, divide, multiply, add, subtract, compare, increment decrement etc.

For example:

var oneThird = Fraction(1, 3);
var reciprocal = oneThird.Reciprocal();

Console.WriteLine(oneThird * reciprocal) : "1"
Console.WriteLine(++oneThird) : "4/3" - just like with an integer ++ adds 1 
Console.WriteLine(oneThird * oneThird) : "1/9"


Please feel free to contribute to the codebase if you feel like it


Parsing HTML in C#

C# Disciples

Today I stumbled upon a bizarre problem, I wanted to parse an HTML for a site to find out if the site contains any RSS feeds. After some research I found out that finding the RSS feed for a site is not that hard, you have to look for an element that looks like this


“Easy task”, I said, me Mr.Optimistic; “I will just load it up in XElement and using Linq to XML to get the data I want”. BUT guess what the web is filled with crazy HTML that a standard .NET parser such as XElement just gives up on and blows up in flames.

After some heavy head banging to walls and ceilings, I found the solution, HtmlAgilityPack. This open source project lets you load HTML even if it is not in a good shape. With some options HTMLAgilityPack will fix these errors and then you…

View original post 36 more words

Anything Over Anything – Tunneling Sofware

http://AoA.codeplex.com – tunneling software written using the pre-release version of the Rx framework. Currently the implementation only supports http protocol, the exchange of data is done by posting the data in http request and then returning the data accumulated on the server. This is done several times a second and requires an normal one directional http access.

The abstraction of the tunnel makes it easy to tunnel any protocols traffic over another protocol, thus the name – Anything Over Anthing.

Would be very glad if someone could add to this project

Scheduling With Quartz.Net

Sacha's Blog

The other day I have a requirement to schedule something in my app to run at certain times, and at fixed intervals there after. Typically I would just solve this using either a simple Timer, or turn to my friend Reactive Extensions by way of Observable.Timer(..).

Thing is I decided to have a quick look at something I have always known about but never really used, for scheduling, which is Quartz.net, which actually does have some pretty good documentation up already:


For me I just wanted to get something very basic up and running, so I gave it a blast.

Step 1 : Install Quartz.net

This is as easy as installing the following NuGet package “Quartz

Step 2 Create A Job Class

This again is fairly easy thanks to Quartz nice API. Here is my job class

That is all you need for a job really. The…

View original post 72 more words

Application Domains

Application Domains

An application domain is the run-time unit of isolation in which a .NET program runs. It provides a managed memory boundary, a container for loaded assemblies and application configuration settings, as well as delineating a communication boundary for distributed applications. Each .NET process usually hosts just one application domain: the default domain, created automatically by the CLR when the process starts. It’s also possible — and sometimes useful — to create additional application domains within the same process. This provides isolation while avoiding the overhead and communication complications that arise with having separate processes. It’s useful in scenarios such as load testing and application patching, and in implementing robust error recovery mechanisms.

Application Domain Architecture

In most cases, the processes housing the application domains are created implicitly by the operating system — when the user double-clicks your .NET executable file or starts a Windows service. However, an application domain can also be hosted in other processes such as IIS or in SQL Server through CLR integration. In the case of a simple executable, the process ends when the default application domain finishes executing. With hosts such as IIS or SQL Server, however, the process controls the lifetime, creating and destroying .NET application domains as it sees fit.

Continue reading “Application Domains”

A Use for Partial Classes/Structs

The keyword partial has been with us for a while now. Until recently I couldn’t find a use for partial classes/structs unless some of the code is auto generated and you want to make sure you don’t interfere with auto generated code. That is until recently I have been asked to create a struct representing a local date – a data that is not affected by time-zones and lacks a time component,

To make it as close as a native CLR type I had to implement a bunch of interfaces, ISerializable, IEquitable<T>, IXmlSerializable. In addition it had to implement some explicit and implicit convertions, !=, ==, >=, <=, > and < operators. Soon, what seemed like a simple struct grew into couple of thousands lines of code,

There is nothing preventing you from putting all the code into one big file, however I think I found a more elegant solution – use the partial keyword and split one large LocalDate by either the interface being implemented or things it’s implementing. So I ended up with:

  • LocalDate.ISerializable.cs
  • LocalDate.IEquitable.cs
  • LocalDate.Operations.cs
  • LocalData.Conversions.cs

I have also used one of the Visual Studio plugins to nest all of those files under a single LocalData.cs.

So far I couldn’t come back with a single draw back of such approach. Please let me know if there are any!

A new compression algorithm?!

compress-mdI started a project yesterday. The idea is quite simple take some data and embed it into an image. The size of the image directly correlates to the data that you can embed in it. So during my testing I have created a RAR archive with maximum compression level and embedded it into a PNG image.

I tried to keep everything simple and the algorithm follows the following steps:

  • Store size of data in bytes, so we have to convert int to an array of 4 bytes. Easily done with the help of BitConverter
  • Store the name, again the data needs to be converted to bytes

Nothing to be excited about so far.

However once I embedded the already compressed data in RAR format into a PNG image, the size has reduced even further, by 20%! Since PNG is a loss-less format, my only explanation is it does some compression using how the pixels are arranged.

I will do further testing and if it does turn to be true and would definitely write a new compression algorithm. Man I probably already revealed too much!

I will keep you posted.

Blog at WordPress.com.

Up ↑