Author Topic: C# 3.0 automatic properties  (Read 2352 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
C# 3.0 automatic properties
« on: October 30, 2007, 02:35:39 PM »
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: [Select]
public class UserProfile
  {
    public string FName { get; set; }
    public string LName { get; set; }
  }
Reflected
Code: [Select]
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? 

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




MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: C# 3.0 automatic properties
« Reply #1 on: October 30, 2007, 02:50:47 PM »
Me thinks someone at microthoft hath been looking at ruby ....
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: C# 3.0 automatic properties
« Reply #2 on: October 30, 2007, 06:55:39 PM »
These 'template' things are getting out of hand, they remind me of the over use of macros in C :D
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: C# 3.0 automatic properties
« Reply #3 on: October 30, 2007, 07:01:56 PM »
Quote
Here is one of the new features of C# 3.0 & VS2008 (.NET 2.0).

.. 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: C# 3.0 automatic properties
« Reply #4 on: October 31, 2007, 12:53:54 AM »
Quote
Here is one of the new features of C# 3.0 & VS2008 (.NET 2.0).

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

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.