Code Red > .NET

C# 3.0 automatic properties

(1/1)

It's Alive!:
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:

--- Code: --- public class UserProfile
  {
    public string FName { get; set; }
    public string LName { get; set; }
  }

--- End code ---
Reflected

--- Code: ---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;
        }
    }
}

--- End code ---

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? 


--- Code: ---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));
    }
  }
}

--- End code ---



MP:
Me thinks someone at microthoft hath been looking at ruby ....

MickD:
These 'template' things are getting out of hand, they remind me of the over use of macros in C :D

Kerry:

--- Quote ---Here is one of the new features of C# 3.0 & VS2008 (.NET 2.0).
--- End quote ---

.. did you mean <..> VS2008 (.NET 3.5).

I'm waiting to see LINQ statement used from ACad ...

Interesting example of the lambda statement too Dan.

It's Alive!:

--- Quote from: Kerry Brown on October 30, 2007, 07:01:56 PM ---
--- Quote ---Here is one of the new features of C# 3.0 & VS2008 (.NET 2.0).
--- End quote ---

.. did you mean <..> VS2008 (.NET 3.5). 

--- End quote ---

Hi Kerry,

Actually this is still using the .NET 2.0 framework (VS 2008 can target specific frameworks),
anything that’s in the new “language specification” is compatible with older frameworks (2.0+).
Some of the fun stuff like “Extension methods” and “LinQ” do require the 3.5 framework though.

Navigation

[0] Message Index

Go to full version