C#
There are 11 entries for the tag
C#
This is an age old problem and one that’s probably been solved countless times before, but I’m going to write about it anyways. Say you’re writing code like this: <p>You have the following themes:</p>
<ul>
@foreach(var theme in Model) {
<li>@theme.Id</li>
}
</ul>
The natural inclination for the lazy developer is to leave enough alone and stop there. It’s good enough, right? Right?
Sure, when the value of Model.Count is zero or larger than one. But when it is exactly one, the phrase is incorrect English as it should be singular “You have...
In my last blog post, I wrote about the proper way to check for empty enumerations and proposed an IsNullOrEmpty method for collections which sparked a lot of discussion. This post covers a similar issue, but from a different angle. A very long time ago, I wrote about my love for the null coalescing operator. However, over time, I’ve found it to be not quite as useful as it could be when dealing with strings. For example, here’s the code I might want to write: public static void DoSomething(string argument)...
UPDATE: Looks like the CLR already has something similar to what I did here. Meet the latest class with a superhero sounding name, ExpandoObject
Warning: What I’m about to show you is quite possibly an abuse of the C# language. Then again, maybe it’s not. ;) You’ve been warned. Ruby has a neat feature that allows you to hook into method calls for which the method is not defined. In such cases, Ruby will call a method on your class named method_missing. I showed an example of this using IronRuby a while back when I wrote about monkey patching CLR...
A while ago I wrote a blog post about how painful it is to properly parse an email address. This post is kind of like that, except that this time, I take on HTML. I’ve written about parsing HTML with a regular expression in the past and pointed out that it’s extremely tricky and probably not a good idea to use regular expressions in this case. In this post, I want to strip out HTML comments. Why? I had some code that uses a regular expression to strip comments from HTML, but found one of those feared...
Here we are already looking ahead to learn about the language features of C# 3.0 and I am still finding new ways to make my code better with good “old fashioned” C# 2.0.
Like many people, I tend to fall into certain habits of writing code. For example, today I was writing a unit test to test the source of a particular event. I wanted to make sure that the event is raised and that the event arguments were set properly. Here’s the test I started off with (some details changed for brevity) which reflects how I would do this in the old days.
private bool eventRaised...
Last night a unit test saved my life (with apologies). Ok, maybe not my life, but the act of writing some unit tests did save me the embarrasment of an obscure bug which was sure to hit when I least expected it. It is cases like this that made me into such a big fan of writing automated unit tests.
Not too long ago I wrote a C# Akismet API for Subtext. In writing the code, I followed design principles focused on making the API as testable as possible. For example, I applied Inversion of Control (IOC) by having the AkismetClient constructor take...
UPDATE: In my original example, I created my own delegate for converting objects to strings. Kevin Dente pointed out that there is already a perfectly fine delegate for this purpose, the Converter delegate. I updated my code to use that instead. Thanks Kevin! Just shows you the size and depth of the Framework libraries.
My recent post on concatenating a delimited string sparked quite a bit of commentary. The inspiration for that post was some code I had to write for a project. One constraint that I neglected to mention was that I was restricted to .NET 1.1. Today, I revisit...
Update: I also wrote a more generic version using anonymous delegates for .NET 2.0 as a followup to this post.
Here’s one for the tip jar. Every now and then I find myself concatening a bunch of values together to create a delimited string. In fact, I find myself in that very position on a current project. In my case, I am looping through a collection of objects concatenating together three separate strings, each for a different property of the object (long story).
Usually when building such a string, I will append the delimiter to the end of the string I am...
Tyler, an old friend and an outstanding contractor for VelocIT recently wrote a post suggesting one would receive better performance by passing in an array of objects to the Microsoft Data Application Block methods rather than passing in an array of SqlParameter instances. He cited this article.
The article suggests that instead of this:
public void GetWithSqlParams(SystemUser aUser)
{
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@id", aUser.Id)
, new SqlParameter("@name", aUser.Name)
, new SqlParameter("@name", aUser.Email)
, new SqlParameter("@name", aUser.LastLogin)
, new SqlParameter("@name", aUser.LastLogOut)
...
Keyvan Nayyeri has a great tip for how to control the display of a type in the various debugger windows using a DebuggerTypeProxy attribute. His post includes screenshots with this in use.
This is an attribute you can apply to a class, assembly, or struct to specify another class to examine within a debugger window.
You still get access to the raw view of the original type, just in case some other developer plays a practical joke on you by specifying a DebuggerTypeProxy that displays the value of every field as being 42.
Tags: .NET, C#, Debugging, Visual Studio.NET,
Tips
Oh boy are you in for a roller coaster ride now!
Let me start with a question, How do you iterate through a large collection of data without loading the entire collection into memory?
The following scenario probably sounds quite familiar to you. You have a lot of data to present to the user. Rather than slapping all of the data onto a page, you display one page of data at a time.
One technique for this approach is to define an interface for paged collections like so...
/// <summary>
/// Base interface for paged collections.
/// </summary>
public interface IPagedCollection
{
///...