I was told that in c# attributes are not allowed on the auto-implemented properties. Is that true? if so why?
EDIT: I got this information from a popular book on LINQ and could not believe it! EDIT: Refer page 34 of LINQ Unleashed by Paul Kimmel where he says "Attributes are not allowed on auto-implemented properties, so roll your own if you need an attribute"
The easiest way to prove that's wrong is to just test it:
using System;
using System.ComponentModel;
using System.Reflection;
class Test
{
[Description("Auto-implemented property")]
public static string Foo { get; set; }
static void Main(string[] args)
{
var property = typeof(Test).GetProperty("Foo");
var attributes = property.GetCustomAttributes
(typeof(DescriptionAttribute), false);
foreach (DescriptionAttribute description in attributes)
{
Console.WriteLine(description.Description);
}
}
}
I suggest you email the author so he can publish it as an erratum. If he meant that you can't apply an attribute to the field, this will give him a chance to explain more carefully.