Here is one of the new features of C# 3.0 & VS2008 (.NET 2.0).
As you can see in the class UserProfile, I did not need to make the private fields. The compiler will generate the fields for me.
The Class UserProfile:
public class UserProfile
{
public string FName { get; set; }
public string LName { get; set; }
}
Reflected
public class UserProfile
{
// Fields
[CompilerGenerated]
private string <FName>k__BackingField;
[CompilerGenerated]
private string <LName>k__BackingField;
// Properties
public string FName
{
[CompilerGenerated]
get
{
return this.<FName>k__BackingField;
}
[CompilerGenerated]
set
{
this.<FName>k__BackingField = value;
}
}
public string LName
{
[CompilerGenerated]
get
{
return this.<LName>k__BackingField;
}
[CompilerGenerated]
set
{
this.<LName>k__BackingField = value;
}
}
}
It seems like a cool feature for the small quick classes, I don’t think I would use it for complex classes though.
One thing I don’t really care for is that, it doesn’t play well with structs and it is supposed to rid the need for
parameterized constructors. Anyone play with this feature yet? Thoughts?
namespace Testing
{
public class UserProfile
{
public string FName { get; set; }
public string LName { get; set; }
}
public class UltraUserProfile : UserProfile
{
public string Email { get; set; }
}
public static class Test
{
[CommandMethod("test")]
static public void cmdtest()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
// Collection Initializer combined with Object Initializer
var Up = new List<UltraUserProfile>
{
new UltraUserProfile { FName = "Dan", LName = "Marcotte", Email = "NoSpam"},
new UltraUserProfile { FName = "Luis", LName = "TheMaStEr", Email = "NoSpam"},
};
//Object Initializer
Up.Add(new UltraUserProfile { FName = "Mark", LName = "SwampGod", Email = "NoSpam" });
//Lambda =>
Up.ForEach(E => ed.WriteMessage("\n"+E.FName + ", " + E.LName + ", " + E.Email));
}
}
}