.NET Predicates (and a .RemoveAll() example)
What is a .NET Predicate and a C# example on how to use it.
Benefits of using ConnectionStrings Element Instead of AppSettings
With .NET 2.0 we can use .ConnectionStrings, and gain 3 benefits. – We can encrypt our connection strings on the fly using reg_iis (or by other methods), We can add SQL Cache Dependencies, By putting connection strings in the right place, we can take advantage of any new features that come out that use the ConnectionStrings element
Use String.Format (C# and VB.NET) instead of Chopping Strings
String.Format is just so cool. It makes your code less spaghetti like and makes it easier to replace string values or add new values. It also allows you to see from a quick glance what your code does. It also makes it MUCH easier to update your string and add more parameters to it. It works similarly to the printf function in C++ that takes a string, and then is followed by parameters that specify what to plug into the string.
Use TryParse instead of Throwing Exceptions (Try {} Catch {})
Tryparse is a C# function that you can use to convert from a string to another data type such as integer. It allows you to avoid throwing exceptions and catching them. This can be a more efficient way to write your code and avoid clunky exceptions. It returns true if the conversion succeeds, so you can also use it to test if a string is numeric or not.
Do NOT eat exceptions (C# .NET)
Proper exception handling is part of writing good code. Here’s a quick tip – if you don’t want to handle an exception, fine, just leave it., don’t put any try/catch and just let the exception propogate up to it’s caller. If you are not sure why you are using try/catch, maybe you shouldn’t be using [...]
How To Properly Use ?? With Default Values
Let’s define a nullable. You can declare a variable as nullable, for example int ? x; You are saying that the int might have a value, or it might have a null value. This is convenient Now in your code you might want to check and see if its actually null and if it is, [...]
Creating SqlParameters Best Practices
This article summarizes some nice ways to create SqlParameter arrays. When we use SqlHelper, or even without SqlHelper when we use SqlCommands directly, and we want to pass an array of SqlParameters to the function. The method I will discuss does not require you to hard-code the number of elements, nor does it require you [...]