How come automatic property does not work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Author

    How come automatic property does not work?

    I wrote a simple Person class with Visual C# 2008 Express Edition, in
    which I define two properties: FirstName and LastName.

    If I use the new c# automatic property feature, it *compiles with no
    problem*, but I don't get the value through the property.

    If I use the regular old style property definition, then it works
    perfect.

    // does not work
    public string FirstName
    {
    get;
    set;
    }

    // does not work
    public string LastName
    {
    get;
    set;
    }


    // The following works.

    public string FirstName
    {
    get
    {
    return this.firstName;
    }
    set
    {
    this.firstName = value;
    }
    }
    public string LastName
    {
    get
    {
    return this.lastName;
    }
    set
    {
    this.lastName = value;
    }
    }

    How come? Am I not using C# 3.0?
  • Marc Gravell

    #2
    Re: How come automatic property does not work?

    Can you define "don't get the value through the property.", ideally
    with an example? That should be fine... are you sure you haven't left
    the old fields in the class, and are looking at the old fields? (auto-
    props define their own fields that you can't see via your code - you
    should only talk to the properties).

    Marc

    Comment

    • Author

      #3
      Re: How come automatic property does not work?

      On Jun 12, 9:48 am, Marc Gravell <marc.grav...@g mail.comwrote:
      Can you define "don't get the value through the property.", ideally
      with an example? That should be fine... are you sure you haven't left
      the old fields in the class, and are looking at the old fields? (auto-
      props define their own fields that you can't see via your code - you
      should only talk to the properties).
      >
      Marc
      Thank you for your reply. Pasted below is the cs file I am testing.

      using System;
      using System.Collecti ons.Generic;
      using System.Linq;
      using System.Text;

      namespace ConsoleApplicat ion1
      {
      class Person
      {
      private string firstName;
      private string lastName;
      public Person()
      {
      }
      public Person(string fn, string ln)
      {
      this.firstName = fn;
      this.lastName = ln;
      }
      public string FirstName
      {
      get;
      set;

      //get
      //{
      // return this.firstName;
      //}
      //set
      //{
      // this.firstName = value;
      //}
      }
      public string LastName
      {
      get;
      set;
      //get
      //{
      // return this.lastName;
      //}
      //set
      //{
      // this.lastName = value;
      //}

      }
      }

      class P
      {
      public static void Main(string[] args)
      {
      Person p = new Person("John", "Doe");
      Console.WriteLi ne(p.FirstName + " " + p.LastName);
      Console.Read();
      }
      }
      }

      Comment

      • Marc Gravell

        #4
        Re: How come automatic property does not work?

        So exactly what I said, then ;-p

        Throw away the two fields "firstName" and "lastName".
        Update the constructor to talk to the properties (change the case).

        this.FirstName = fn;
        this.LastName = ln;

        Don't stress thinking "but that is less efficient"; first it would be
        completely unnoticeable, and second it is highly likely that the "JIT"
        will "inline" these calls at runtime, making them pretty much
        identical to talking directly to the fields.

        Marc

        Comment

        • Author

          #5
          Re: How come automatic property does not work?

          On Jun 12, 10:03 am, Marc Gravell <marc.grav...@g mail.comwrote:
          So exactly what I said, then ;-p
          >
          Throw away the two fields "firstName" and "lastName".
          Update the constructor to talk to the properties (change the case).
          >
          this.FirstName = fn;
          this.LastName = ln;
          >
          Don't stress thinking "but that is less efficient"; first it would be
          completely unnoticeable, and second it is highly likely that the "JIT"
          will "inline" these calls at runtime, making them pretty much
          identical to talking directly to the fields.
          >
          Marc
          Hokay, :-) Thank you very much. I didn't know that we *must not* use
          private fields for properties.

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: How come automatic property does not work?

            On Jun 12, 3:15 pm, Author <gnewsgr...@gma il.comwrote:
            Hokay, :-) Thank you very much. I didn't know that we *must not* use
            private fields for properties.
            It's not a case that you "must not use private fields" for properties.
            It's that automatically implemented properties introduce their own
            (hidden) fields - they won't start implicitly using ones you've
            declared.

            Jon

            Comment

            • Marc Gravell

              #7
              Re: How come automatic property does not work?

              Hokay, :-) Thank you very much.  I didn't know that we *must not* use
              private fields for properties.
              Only for auto-implemented properties. Simply, that isn't what the
              compiler is going to talk to, so you had (roughly speaking - the names
              are very different) the code below.

              Marc

              class Person {
              private string firstName, lastName;
              private string compilerGenerat ed1, compilerGenerat ed2;

              public string FirstName {
              get {return compilerGenerat ed1;}
              set {compilerGenera ted1 = value;}
              }
              public string LastName {
              get {return compilerGenerat ed2;}
              set {compilerGenera ted2 = value;}
              }
              public Person() {}
              public Person(string fn, string ln) {
              firstName = fn;
              lastName = ln;
              }
              }

              Comment

              • Martin Bonner

                #8
                Re: How come automatic property does not work?

                On Jun 12, 3:15 pm, Author <gnewsgr...@gma il.comwrote:
                On Jun 12, 10:03 am, Marc Gravell <marc.grav...@g mail.comwrote:
                >
                So exactly what I said, then ;-p
                >
                Throw away the two fields "firstName" and "lastName".
                Update the constructor to talk to the properties (change the case).
                >
                this.FirstName = fn;
                this.LastName = ln;
                >
                Don't stress thinking "but that is less efficient"; first it would be
                completely unnoticeable, and second it is highly likely that the "JIT"
                will "inline" these calls at runtime, making them pretty much
                identical to talking directly to the fields.
                >
                Marc
                >
                Hokay, :-) Thank you very much. I didn't know that we *must not* use
                private fields for properties.
                That should be:
                "must not use private fields for *automatic* properties".

                If you want to directly access the private fields - write the
                properties out in full. Personally, I usually find I want to do that
                anyway, because I want to fire events when a property changes, or I
                want to calculate the return value, or something.

                Comment

                Working...