Category: .NET

"using", the safe keyword

Frequently, we don't really care what the exception is, but if it is thrown we want to finalise operations e.g. close the database connection or files. This is where using comes in. using keyword allows to make code a little bit more readable by omitting try-catch-finally clause. Instead, your object should implement IDisposable interface and

Continue Reading

Neat way of getting rid of nullable type in Linq

Nullable types can sometimes be a handful to work with. This 'billion dollar mistake' (as described by Tony Hoare who claims to have invented null) can, however, be convereted to r There is a need way of converting nullable type: ??. The example below selects some value from an object using Select method. YourVariable is

Continue Reading

foreach image in a resource file

Resource files are a very useful addition to the .NET framework as they allow easy storage and retrival of data e.g.

In order to iterate through the all the resources in the resource file, we have to use ResourceSet. To get it we need run a resource manager on our resource file. The complete

Continue Reading

How to display an image on the website from a binary source (e.g. a database)

There are various ways in which one could display an image on the website form a binary source. One solution would invole simply converting the binary source into a base64 and put it as the src of the image:

However, this is not acceptable if you load images 'on demand' i.e. when the user

Continue Reading

"The target application has been unloaded" - the annoying error

"The target application has been unloaded" is highlighted by Visual Studio, but doesn't actually do anything. Interestingly enough the error wasn't doing anything. It was just highlighted by Visual Studio and prevented the syntax highlighter as well as the code builder from working. After finding nothing on the internet I gave up and went home.

Continue Reading

Tracing of WCF services - code example

Tracing can be very beneficial especially when the application has already been deployed and we need to figure out why things do work. Useless errors of type HTTP 500 don't give any meaningful results, so I decided to use System.Diagnostics to help me track down the errors. Logging There are two important (and very simplifed

Continue Reading

Using InverseProperty and ForeignKey Attributes

InverseProperty and ForeignKey are (arguably) the most commonly used attribues in Entity Framework when developing in code-first approach. They help greatly simply the job of managing our models. Foreign Key In order to introduce foreign key to our code we need to specify to what objects it maps to. So there are two things that

Continue Reading

Removing Entry from a Database using Entity Framework

Most of the tutorials on the internet seem to focus on how to remove these objects from DataContext as opposed to DbContent. However, if your database is built using code first approach you are unlikely to be using DataContext/. In order to delete an entry from the database, firstly Remove the element and then save

Continue Reading

Proper way of returning multiple values from a method

Since C# 7 you can make method signatures which correspond to multiple values In Python, you can return multiple values from a function. This functionality is not however built-in into C#, shame. I think this is a nice feature of the language. It frequently reduces asymptotic complexity (i.e. "performance") without resorting to implementing another class

Continue Reading

Using #region to improve readability of your Code in VS

#region is an interesting instruction that tells Visual Studio how to wrap the code into 'meaningful' chunks. In larger classes, especially, those with several interfaces it is particularly helpful.

Pretty handy if you ask me. The downside is, and I have seen this already a couple of times, is that some engineers use them

Continue Reading