Properties intitialized in ctor

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

    Properties intitialized in ctor

    Why cannot properties be initialized in a constructor, especially read-
    only onces.

    public class {

    pubic string Name {get, readonly set;}

    }
  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: Properties intitialized in ctor

    You can do this but not with automatic properties, you should just create
    your own backing store and make it a readonly (just a get) properties which
    returns a readonly variable.

    --
    Ciaran O''Donnell
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "puzzlecrac ker" wrote:
    Why cannot properties be initialized in a constructor, especially read-
    only onces.
    >
    public class {
    >
    pubic string Name {get, readonly set;}
    >
    }
    >

    Comment

    • Marc Gravell

      #3
      Re: Properties intitialized in ctor

      Typos aside, yes - it would be very nice to be able to declare
      readonly auto-implemented properties, especially for writing immutable
      classes. Presumably it would have to be a "private readonly" to make
      sense. However, this simply isn't in the language at the moment.

      You have two choices: for a true immutable, do it the long way:

      private readonly string name;
      public string Name {get {return name;}}
      (and assign this.name in the ctor)

      For a semi-immutable object, make the set private and simply don't set
      it after the ctor:

      public string Name {get; private set;}
      (and assign this.Name in the ctor)

      Marc

      Comment

      • Peter Morris

        #4
        Re: Properties intitialized in ctor

        If it is readonly then it is a field and not a property....

        public class Person
        {
        public readonly string Name;
        }


        Pete

        Comment

        • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

          #5
          Re: Properties intitialized in ctor

          In my opinion its still nice for encapsulation to have this as a property, to
          shield the outside world from the source of the data. It could be possible in
          future to change this to make Name = firstname + " " + lastname; and the
          property would hide this from consumers.


          --
          Ciaran O''Donnell
          try{ Life(); } catch (TooDifficultException) { throw Toys(); }



          "Peter Morris" wrote:
          If it is readonly then it is a field and not a property....
          >
          public class Person
          {
          public readonly string Name;
          }
          >
          >
          Pete
          >
          >

          Comment

          • Peter Morris

            #6
            Re: Properties intitialized in ctor

            In my opinion its still nice for encapsulation to have this as a property,
            to
            shield the outside world from the source of the data. It could be possible
            in
            future to change this to make Name = firstname + " " + lastname; and the
            property would hide this from consumers.
            At which point I would change it from a field to a property. I always use
            fields for readonly because it saves typing, unless I need to use the class
            as a datasource and therefore it must be a property, but then I usually need
            a setter that does nothing just to get a column to show anyway.


            Pete

            Comment

            • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

              #7
              Re: Properties intitialized in ctor

              The only issue with changing it when the needs change is that you would need
              to rebuild the consumers even though you wont need to change their code.
              Having it as a property from the start means you could just deploy a new
              version of the assembly containing this code.

              --
              Ciaran O''Donnell
              try{ Life(); } catch (TooDifficultException) { throw Toys(); }



              "Peter Morris" wrote:
              In my opinion its still nice for encapsulation to have this as a property,
              to
              shield the outside world from the source of the data. It could be possible
              in
              future to change this to make Name = firstname + " " + lastname; and the
              property would hide this from consumers.
              >
              At which point I would change it from a field to a property. I always use
              fields for readonly because it saves typing, unless I need to use the class
              as a datasource and therefore it must be a property, but then I usually need
              a setter that does nothing just to get a column to show anyway.
              >
              >
              Pete
              >
              >

              Comment

              Working...