C# Coding conventions

These coding conventions were originally written by my colleagues Filip Kassovic and Michal Třešňák in ST-Software. I have only made slight updates and published it on my blog. Do not use “var” keyword when not necessary var response = SomeClass.SomeMethod(input); Reason we don’t see the type of “response” “Find Usages” or “Find all references” doesn’t find usage of type when using “var” Exceptions obviously, you have to use it for anonymous types var anonymous = new { A = 1, B = 2 }; Do not use complex object initializer PaymentTransaction transaction = new PaymentTransaction { Token = statusResponse.RefNo, Error = …

Continue Reading

C# – How to find overlap of datetime intervals

I needed to count the overlap of the datetime intervals. After some coding I’ve refactored the code to static class and uploaded to Github, so that others can use it too. It might save you some time 🙂 There are currently two methods. One to get the interval overlap and the other to count the interval duration. Go ahead and check out our company GitHub. The code is pretty straight forward, so no need to explain too much.

Continue Reading

How to speed-up generating PDFs via TMS Flexcel

In my current project we needed to generate lot of PDFs from the same excel template.  For this we use TMS Flexcel library. It is a simple library for generating Excel or PDF files in .NET. It loads a xls template from filesystem, populates it with data and that’s it. Quite easy. But generating tons of files in a cycle can be a pain, because the template has to be loaded from file system in every iteration. That can be performance problem. The solution is to read the template into MemoryStream and then in each iteration create new XlsFile object from this memory stream. …

Continue Reading

StaticExtension not found

I have encountered funny bug/feature when I was writting first xaml file in my new project. I used the StaticExtension the way I was used to… and for some obscure reason it did not work. This is what I typed: <Someclass SomeProperty=”{x:Static SomeOtherClass:SomeStaticProperty}”/> But Visual Studio did not like it and complained that Static is not recognized. I doubled checked project references, compared them with those of last project and did not find anything what could have caused it. After some googling I read that the Static is actually named StaticExtension. The word ‘static’ is just some kind of shortcut. …

Continue Reading

How to get text value from HtmlTextWriter in C#

HtmlTextWriter is very nice tool to generate HTML in C#, but it lack ability to get generated HTML as a string. Here is a simple solution: HTmlTextWriter uses internally StringWriter which uses StringBuilder. So you first create StringBuilder and StringWriter, pass them to HtmlTextWriter and when you are done you simply call ToString() on the instance of StringBuilder.  

Continue Reading