Tips

.Net Tips – always override ValueType.Equals

You should already know that in C# all types derive from System.Object and that C# supports both reference types (which are allocated onto the heap) as well as value types (primitives, enum, struct, etc. which are allocated onto the stack). One of the key differences between value types and references types is that when you …

.Net Tips – always override ValueType.Equals Read More »

.Net Tips – using InternalsVisibleTo attribute to help testing non-public methods

Oh, the Pain! For a long time, unit testing non-public methods in C# has been a pain in the back side. In order to test a non-public method (most likely private methods) you have a number of choices each with an undesired effect: The easiest way out is to make the methods public. However, this …

.Net Tips – using InternalsVisibleTo attribute to help testing non-public methods Read More »

.Net Tips – making a serializable immutable struct

As you might know already, an object is immutable if its state doesn’t change once it has been created. In C# the most used immutable type is string, this means every time you modify the value of a string variable you are actually creating a new string object and updating the reference of the variable …

.Net Tips – making a serializable immutable struct Read More »

.Net Tips – marking the default enum value ‘Undefined’

enum is a tremendously useful structure in C#, but when defining a new enum there is one thing you should always ask yourself: What should the default value be for my enum? The problem with default value is that enum is really an int underneath, and as do all integers in C# an enum has …

.Net Tips – marking the default enum value ‘Undefined’ Read More »

.Net Tips – converting between Enum and string

If you’re reading this then you should already know what an enum is in C# and that the underlying type of an enum element is int and you can convert between int and enum using casting: However, sometimes you want to convert an enum element to and from strings instead of ints because: they’re more …

.Net Tips – converting between Enum and string Read More »

.Net Tips – using as vs casting in C#

Runtime type conversion is something we all have to do from time to time, and in C#, type conversion is usually done using either the as keyword or casting. Here’s a quick glance of how the two approaches differ: Null reference Conversion failure User-defined conversion Performance as Null Null Ignored Fast casting Null Exception Used …

.Net Tips – using as vs casting in C# Read More »

.Net Tips – using readonly vs const in C#

In C#, there are two ways for you to declare a constant variable, you can either declare the variable as readonly, or const: readonly A variable declared with the readonly modifier can only be assigned as part of the declaration or in the class’s constructor: const A variable declared with the const modifier must be …

.Net Tips – using readonly vs const in C# Read More »

Castle Windsor Tips – specifying generic types in config file

If you have an interface like this: with a concrete class like this, which you want to wire up with Castle: Then here’s the syntax to specify the component in your Castle Windsor configuration file Similarly, imagine if you have more than one generic type parameter: Remember, you need to use the back tick (`), …

Castle Windsor Tips – specifying generic types in config file Read More »

WPF – how to bind ScaleTransform to Slider, percentage style!

Whilst working on an image view I needed the ability to bind the value of a Slider to a ScaleTransform as a percentage value and at the same time display the value of the slider in a label. So for a zoom range from 1% to 250% you would usually set the Minimum and Maximum …

WPF – how to bind ScaleTransform to Slider, percentage style! Read More »

WPF – loading grab and grabbing cursors from resource

Of all the cursors that we come across on a daily basis, the grab and grabbing cursors are two notable absentees in the list of supported cursors in WPF/Silverlight. So if you happen to need these two cursors as I did earlier in the day, then here’s a few easy steps to get you going: …

WPF – loading grab and grabbing cursors from resource Read More »

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close