dotnet


5
Jun 08

C# Automatic Properties can have Attributes

The MSDN documentation for Automatic Properties (a C# 3 language feature) states that attributes are not valid on automatic properties. This is not true, the following code compiles and works as expected:

public class Data
{
[XmlAttribute]
public int SomeNumber { get; set; }
}

class Program
{
static void Main(string[] args)
{
using (XmlWriter w = XmlWriter.Create(@”c:\temp\test.xml”))
{
XmlSerializer s = new XmlSerializer(typeof (Data));
s.Serialize(w, new Data { SomeNumber = 5 });
}
}
}

Running this code results in an XML file looking roughly like this (I stripped some unneeded namespace declarations):

< ?xml version="1.0" encoding="utf-8"?>
<data SomeNumber="5" />

SomeNumber is saved as an attribute, so the XmlAttribute attribute worked correctly.


28
May 08

Microsoft Source Analysis Released

My team at work uses a fantastic Visual Studio add-in called Source Analysis to ensure consistency in our C# code formatting, commenting and organisation.

Microsoft Source Analysis

Source Analysis has now been released for the whole world to use, for free! The free download is available at the MSDN Code Gallery:

Source Analysis at Code Gallery

There’s also a blog here:

Source Analysis Blog