Contents tagged with Tips
-
Randomizing LINQ to SQL queries
Yesterday, a developer asked in the LINQ in Action forum and in Microsoft's official LINQ forum how to write a LINQ to SQL query that would return random records from the database.
-
Rethrowing exceptions and preserving the full call stack trace
Did you know that depending on the way you rethrow exceptions you may lose important information? There are already several blog posts that explain and demonstrate the difference between throw and throw ex. I'm realizing only now that none of the two solutions yields the complete call stack trace information!
-
Hunting down bad try..catch blocks
Way too often developers take the easy solution to use try..catch blocks to silence and ignore exceptions.
-
Use the power of let in your LINQ queries
Often, when you try to find out how to write the correct LINQ query you need, you end up being confused because it becomes too complex. In such situations, you should remember that the let clause is here to help you.
-
Sending the LINQ to SQL log to the debugger output window
When debugging LINQ to SQL code, did you wish you could easily see the SQL that gets executed? Of course there is the DataContext.Log property that is available for that. You can assign any TextWriter to this property. Console.Out is a good candidate for example, but it doesn't help much for web applications and doesn't integrate with Visual Studio nicely. Fortunately, Kris Vandermotten has a nice solution: DebuggerWriter. It's an implementation of TextWriter that writes to the debugger log.
-
Rationalize your build process with code mobility
Patrick Smacchia, C# MVP, author of the best-seller Practical .NET2 and C#2 and creator of NDepend, has started to blog.
-
Using configSource to split configuration files
As Nikhil Kothari writes in his blog, he's started to use the configSource attribute to split configuration files into smaller pieces.
-
Which type should I use in C# to represent numbers?
Luca Bolognese, from the Microsoft C# team, has an interesting post that aims at providing answers to the following question: Which type should I use in C# to represent numbers?
Luca and the C# team try to provide a simple algorithm that can help when you are confused about the numeric types in .NET. It may not address every scenario, but it can be useful when you are lost between byte, short, int, uint, long, float, double, decimal, and their friends. -
Exception handling and resource protection with try..finally
Paul Sheriff has just published an example he uses to recommend the use of try..finally blocks. Here is his example:
-
Removing diacritics (accents) from strings
It's often useful to remove diacritic marks (often called accent marks) from characters. You know: tilde, cédille, umlaut and friends. This means 'é' becomes 'e', 'ü' becomes 'u' or 'à' becomes 'a'. This could be used for indexing or to build simple URLs, for example.
Doing so is not so easy if you don't know the trick. You can play with String.Replace or regular expressions... But do you know .NET 2 has all that is required to make this easier?