Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, November 2, 2014

Getting Started with T4 templates

Recently, I had to troubleshoot an issue that involved TDS code generation of Glass mapper items. This got me looking at T4 templates, and I have been struck at how cleanly this is implemented and how good the MSDN documentation is (http://msdn.microsoft.com/en-us/library/dd820620.aspx, which some of the examples below are from.)  I like stuff you can get up and running in a few minutes.

Monday, January 28, 2013

Why All Those Lambdas in NUnit Tests?

I'm sure this is obvious, but I just figured it out. Suppose you are testing to verify that an expected System.Exception is thrown. Why does the first of these tests fail, but the second one passes?

Sunday, September 2, 2012

Using enums as constants

The other day I found myself having to add a value in to a dictionary with a number of constant keys. I started out with this:

//populating method:
dictionary["constant1"] = value1;
dicitonary["constant2"] = value2;
...
//consuming method:
value1 = dictionary["constant1"];
value2 = dictionary["constant2"];

Then I felt bad.

Monday, March 19, 2012

Looking at LINQ

I've been digging through the discussion of LINQ in the Albahari brothers' C# 4.0 in a Nutshell: The Definitive Reference, and it has cleared up a number of points for me:
  1. At a compiler level, LINQ is a series of transformations done to expressions that begin with FROM and end with SELECT or GROUP BY.  These transformations turn free text queries into extension methods.  For example, var query = from A in aList select A.ToUpper() is transformed by the compiler into var query = aList.Select(a => a.ToUpper()). But, and this is a key point, the implementation of the methods is not determined.  You determine the implementation by the namespaces you include; you could write your own implementation of Select, Where, etc.  

Saturday, November 12, 2011

Creating a Gutter Icon for Proxies

One of the true joys of Sitecore is the ease of customization.  We use proxies fairly extensively on our site, and a colleague recently complained that the Content Editor did not give a clear indication of whether an item is a proxy, and what is its source.  This inspired me to see if I could add a gutter icon to identify proxies, similar to the gutter clones icon that ships with Sitecore 6.4.   (The "gutter" is the area to the left of the content tree with icons, such as "My Locked Items", that can be toggled on and off.)  Like the clones icon, I wanted one that would navigate to the source item when clicked.  This is a fairly straightforward task, but I did have to lean on ReSharper's decompiler to see how the Sitecore guys built the clones icon.

Saturday, July 16, 2011

Structs in C#

Just read a very good discussion on structs in Professional C# 4.0 and .NET 4 (Wrox Programmer to Programmer).

Here are the main points:
  • structs are value types, so they get created and destroyed very fast.
  • They are well suited for grouping together a small number of value fields, such as a pair of ints for X,Y coordinates.
  • Because they are values, they do not need to be initialized with the new keyword.   
  • You cannot override the default constructor, nor initialize fields inside a struct, except in a non-default constructor.
  • If you create an instance of the new struct with new, the fields are initialized to their default values.  If you create the struct without new, you must manually initialized the fields before using them.
Poking around with this some more, I've noticed that creating a non-default constructor does not hide the default constructor, unlike classes.