January 27th, 2008
I have passed my first MS exam
Although it covers many aspects of .NET, it is the most stupid exam I have ever taken (Econometrics which I passed on a 6th attempt does not count :D)
Questions are very confusing and when you do not memorize most of .NET classes and methods you will probably fail. Many possible answers differ only by method signatures and you need to know exactly when the parameter A must be on position 1 and the parameter B must be on position 2. I am a little bit disappointed because there were no questions about designing and solving problems. IMHO this kind of exam does not prove that a programmer has any useful skills.
Anyway, I’m glad I could make it 
December 18th, 2007
Every programmer must sometimes use a debugger
In fact, most of our job is filled with debugging rather than writing code.
In the .NET framework there are many great features we can use to make us life easier. Today I am gonna present you a few very useful things from System.Diagnostics namespace.
Debugger class:
This is a class which allows us a communication with a attached debugger. It has multiple members but there are 3 of them I want you to notice.:
- Break - It just sends a break signal to a debugger. Very useful when we want to conditionally stop the execution of our application
- Launch - It can launch a debugger and attach it to a process
- Log - It posts a message to the attached debugger
IMHO, the most useful of those three methods is Break. Priceless when we have an unexpected situation in a code. Thanks to this method we are able to break an execution of a program. Look at this snippet:
C#:
object foo = Bar.GetSomething();
if(foo == null)
{
Debugger.Break();
}
Cool, isn't it?
Debug class:
Another good class which provides us helper methods in debugging process. Noticeable are:
- Assert - Evaluates a condition and displays a message if a condition is false. Well known for a "test driven" programmers.
- Fail - Outputs a failure message. Quite similar to the previous one but it doesn't evaluate an expression.
- Indent and Unident - useful for output formatting.
- Write, WriteLine and Print - write information to the Debug / Trace listeners
- WriteIf, WriteLineIf - The same as above but only when specified condition is met
Ok, those classes are in fact primitive but here is something that is very useful in debugging complex hierarchical objects.
Ladies and Gentleman - please welcome Debug attributes. Thanks to them a developer is able to declaratively specify how an application should behave during debugging process.
Let's say that you do not want a debugger step into a piece of code (because it's tested and works well) - no problem, there is DebuggerHiddenAttribute. Actually it stops a breakpoint from being set inside anything that it decorates.
C#:
class Foo
{
[DebuggerHidden
()]
public void Bar
()
{
// impossible to debug here
}
}

Let's say that we want to determine how a class or a property is displayed in a debugger window. There are two other attributes: DebuggerBrowsableAttribute and DebuggerDisplayAttribute. This code will do a trick with local variable / watch window in Visual Studio:
C#:
[DebuggerDisplay
("Our Foo class")]
class Foo
{
[DebuggerBrowsable
(DebuggerBrowsableState.
Never)]
private int _bar =
10;
[DebuggerDisplay("Value of Bar is: {_bar}")]
public int Bar
{
get
{
return _bar;
}
}
}
and a debugger view:

Nice
You can ofcourse set some parameters to those attributes. There are more attributes such as DebuggerStepThroughAttribute but I recommend further reading on MSDN.
October 26th, 2007
Every coder sometimes encounters a situation when a bug must be fixed. At the begining it looks for a very easy task... but the code reveals that it might be quite tricky
Here is one of most common antipattern - hardcoding mixed with magic numbers. Please fasten your seatbelts
C#:
EditorDatabase EditorDB =
new EditorDatabase
();
if (UserIDTextBox.
Text ==
"0" && LoginRoleDropDownList.
SelectedValue ==
"11")
{
EditorDB.
Save(CodeHiddenField.
Value, TextGridView.
Rows[rowID
].
Cells[5].
Text,
System.
Convert.
ToInt32(TextGridView.
Rows[rowID
].
Cells[10].
Text), TextGridView.
Rows[rowID
].
Cells[8].
Text,
0, spr, TextGridView.
Rows[rowID
].
Cells[11].
Text).
ExecuteNonQuery();
}
else
{
EditorDB.
Save(CodeHiddenField.
Value, TextGridView.
Rows[rowID
].
Cells[5].
Text,
System.
Convert.
ToInt32(TextGridView.
Rows[rowID
].
Cells[10].
Text), TextGridView.
Rows[rowID
].
Cells[8].
Text,
System.
Convert.
ToInt32(UserDropDownList.
SelectedValue), spr, TextGridView.
Rows[rowID
].
Cells[11].
Text).
ExecuteNonQuery();
}
August 19th, 2007
... when you define a value type (structure) it's a good idea to override System.Object's virtual methods - ToString, Equals and GetHash. If you don't, the framework will perform "boxing" when calling these methods. It can significantly slow your application.
August 12th, 2007
... that simple strings concatenating in C# can have impact on memory usage?
Let's look at this simple code:
C#:
string foo = "foo";
foo += " bar";
foo += " bar2";
foo += " bar3";
This causes that the compiler creates 3 additional strings on the stack, then garbage collector must clean it. Only the last string has a reference. If you make something like this very often in a loop consider using string's Concat or Join methods. StringBuilder is also a good choice.