Archive for the ‘C#’ Category

Amusing Bad Programming Tip

Say you have a class with a property that’s a string. Using object initializers you can do this little nugget:

Product p = new Product { ProductName = Console.ReadLine() };

It will work as you’d expect (prompt you for input, then assign it to ProductName). However, that’s pretty easy to miss when you’re quickly scanning code. [...]


Use LINQ’s Select and Initializers to Make Mapping Easier

Mapping data between types on a Web Service can be a collosal pain. There are various frameworks out there that can speed things up, but sometimes you just need to do it by hand. Fortunately some of the features introduced with C# 3 can help speed things up.
Say the type on the server looks like [...]


More Fancypants LINQ Fun

Say you had an object model that was generated from a database like so:
public class Lecture {
public List<Speaker_Lecture> Speakers { get; set; }
}

public class Speaker_Lecture {
public Lecture Lecture { get; set; }
public Speaker Speaker { get; set; }
}

public class Speaker {
public string Name { get; set; }
}
Unfortunately, you’re stuck with that semi-ugly Speaker_Lecture model, and [...]


Different Action Results with ASP.NET MVC and jQuery

I’m a big fan of the ASP.NET MVC framework. I love the fine grained control it gives you over everything. But what I love most is that you can unleash the fully armed and operational firepower of jQuery.
Recently, I’ve been working on a site that is using both MVC and jQuery to make AJAX calls. [...]


LINQ To SQL and Tight Coupling Part 4

I kind of left my last post on this subject dangling, but now I’m ready to come clean.  I was proceeding merrily along, removing the tightly coupled LINQ to SQL generated code into something nice and loose, but ran into some serious snags when trying to update the database objects that had joins associated with [...]


Delegation Part 2

Back in Part 1, we covered the history of Delegates in the .NET Framework. As stated, in 1.1, they were a little too confined to be exceptionally useful. However, in the 2.0 Framework, Delegates were given the ability to be "Anonymous" and could be created on the fly. This allows for more elegant coding, and [...]


Delegation Part 3

Delegates have come a long way since they were introduced in .NET 1.0. Back then they were only a way to refer to existing methods as variables. 2.0 allowed methods to be inlined, making them "anonymous." The 3.5 Framework does not introduce any new concepts regarding delegates, but it makes writing them much, much quicker [...]


Delegation

Delegates are a big part of the .NET Framework. They were included since the beginning with .NET 1.0 and have matured since then, but in my experience I don’t see them used intentionally too often.
I think the reason for this is because the concept of a delegate is kind of foreign if you have a [...]


C# 3.0's argumentless lambda

One of the hip new features of C# 3.0 is the built in support for lambdas:
Func<int> sq = x => x * x;
sq(5); // equals 25
This is very fancy, but sometimes you need to perform a lambda that has no argument. The syntax for that is simply "()".
Func<object> doStuff = () => Console.WriteLine("stuff has been [...]


LINQ To SQL and Tight Coupling Part 3

Last time, we tweaked our code to make it a little easier to understand and maintain. So that means we get to dirty it up again!  Our current object model has very simple mapping of one table to one class, with no associations between them. In the real world, this doesn’t happen too often (and [...]