Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C# Object Initializer Syntax

C# Object Initializer Syntax

Scheduled Pinned Locked Moved The Lounge
csharpvisual-studio
45 Posts 22 Posters 51 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Munchies_Matt

    I always use your second style here, all function calls are broken up with one param per line, all neatly lined up. (I am so anal about code tidyness :) )

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #23

    Munchies_Matt wrote:

    I am so anal about code tidyness :)

    ... and sooo paid by the LOC it seems ;)

    1 Reply Last reply
    0
    • B Brady Kelly

      Or the very subject of this topic, an object initializer: StudentName student2 = new StudentName {"Craig", "Playstead"};

      Do what thou wilt shall be the whole of the Law. - Liber AL vel Legis 1:40, Aleister Crowley

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #24

      Give it another iteration of new language features and we'll probably have the even shorter C++ version. At times I wish they went faster but at least MS is steadily chipping away at language verbosity in C#; unlike Sun/whOracle who seem to revel in Java's bloatyness. :doh:

      Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

      K B 2 Replies Last reply
      0
      • Z ZurdoDev

        Chris C-B wrote:

        Anything that gets rid of squirly brackets has got to be good.

        Long live VB! :thumbsup::cool:

        There are only 10 types of people in the world, those who understand binary and those who don't.

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #25

        My thoughts exactly! :laugh:

        Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

        Regards, Sander

        1 Reply Last reply
        0
        • J Jacquers

          Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

          T Offline
          T Offline
          TheGreatAndPowerfulOz
          wrote on last edited by
          #26

          I like the first. Less typing and more readable.

          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

          1 Reply Last reply
          0
          • L Lost User

            Not only that. It does not introduce any real benefit. Sure, the inititialisation now is atomic, but that can become a pain when debugging (which one of those 4000 fields threw that exception during the initialisation?) and actually is not needed so often.

            The language is JavaScript. that of Mordor, which I will not utter here
            This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
            "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #27

            CDP1802 wrote:

            4000 fields

            :omg: I think I've located your problem.

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            L 1 Reply Last reply
            0
            • I Ian Shlasko

              CDP1802 wrote:

              4000 fields

              :omg: I think I've located your problem.

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #28

              It were only 150, but that already is bad enough.

              The language is JavaScript. that of Mordor, which I will not utter here
              This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
              "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

              I 1 Reply Last reply
              0
              • L Lost User

                It were only 150, but that already is bad enough.

                The language is JavaScript. that of Mordor, which I will not utter here
                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                I Offline
                I Offline
                Ian Shlasko
                wrote on last edited by
                #29

                Yeah, that's just... ouch... If my objects ever have more than a couple dozen fields, I start thinking of the most logical way to split them up. But if you're manually initializing 150 fields at once, whether it's in one object or an object tree, it's time to rethink your design.

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                L 1 Reply Last reply
                0
                • L Lost User

                  I try to avoid such an unreadable mess where I can. Initializing objects with many members within some other construct is an unreadable mess.

                  The language is JavaScript. that of Mordor, which I will not utter here
                  This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                  "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                  T Offline
                  T Offline
                  TheGreatAndPowerfulOz
                  wrote on last edited by
                  #30

                  If that causes an "unreadable mess", may I suggest that indicates a different problem.

                  #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                  L 1 Reply Last reply
                  0
                  • T TheGreatAndPowerfulOz

                    If that causes an "unreadable mess", may I suggest that indicates a different problem.

                    #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #31

                    Yes, and that was the least of their problems. They also found it nice and well that the application logic resided under the data access layer, in stored procedures and triggers.

                    The language is JavaScript. that of Mordor, which I will not utter here
                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                    T 1 Reply Last reply
                    0
                    • I Ian Shlasko

                      Yeah, that's just... ouch... If my objects ever have more than a couple dozen fields, I start thinking of the most logical way to split them up. But if you're manually initializing 150 fields at once, whether it's in one object or an object tree, it's time to rethink your design.

                      Proud to have finally moved to the A-Ark. Which one are you in?
                      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #32

                      The database already was a horror by itself, but could not be changed without risking a complete collapse. They were simply attempting to add something like data access classes and initializing data objects from data query results. It was a feeble attempt to bring some order to this application because a good part of the application logic was hidden in database triggers. Doing CRUD operations with data access objects usually had unexpected side effects.

                      The language is JavaScript. that of Mordor, which I will not utter here
                      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Yes, and that was the least of their problems. They also found it nice and well that the application logic resided under the data access layer, in stored procedures and triggers.

                        The language is JavaScript. that of Mordor, which I will not utter here
                        This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                        "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                        T Offline
                        T Offline
                        TheGreatAndPowerfulOz
                        wrote on last edited by
                        #33

                        X| X| X| X| X|                         X| X| X| X| X| X|                             X| X| X| X| X| X| X| X| X|               X| X|                             X| X|               X| X| X| X| X| X| X| X| X| X| X| X|           X|                                                X|      X| X| X| X| X| X| X| X| X| X| X| X| X| X|      X|                                                     X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X|      X|               X|      X|                    X| X| X| X| X| X|               &

                        L 1 Reply Last reply
                        0
                        • T TheGreatAndPowerfulOz

                          X| X| X| X| X|                         X| X| X| X| X| X|                             X| X| X| X| X| X| X| X| X|               X| X|                             X| X|               X| X| X| X| X| X| X| X| X| X| X| X|           X|                                                X|      X| X| X| X| X| X| X| X| X| X| X| X| X| X|      X|                                                     X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X|      X|               X|      X|                    X| X| X| X| X| X|               &

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #34

                          Right.

                          The language is JavaScript. that of Mordor, which I will not utter here
                          This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                          "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                          1 Reply Last reply
                          0
                          • J Jacquers

                            Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

                            R Offline
                            R Offline
                            Rage
                            wrote on last edited by
                            #35

                            Initializing is for sissies. Real programmers do not initialize.

                            Do not escape reality : improve reality !

                            1 Reply Last reply
                            0
                            • J Jacquers

                              Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

                              K Offline
                              K Offline
                              Kiriander
                              wrote on last edited by
                              #36

                              I woud simply use a parametrized constructor to construct and initialize the instance at once. var student2=new StidentName("Craig","Playstead");

                              1 Reply Last reply
                              0
                              • J Jacquers

                                Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

                                M Offline
                                M Offline
                                Middle Manager
                                wrote on last edited by
                                #37

                                Sadly I've been corrupted by javascript and I am currently finding this the most readable (and I do this for SQL as well for INSERT INTO column lists)

                                StudentName student2 = new StudentName {
                                FirstName = "Craig",
                                LastName = "Playstead"
                                };

                                I know, I've gone rabid and need to be put down out of humane concern. :sigh:

                                L 1 Reply Last reply
                                0
                                • D Dan Neely

                                  Give it another iteration of new language features and we'll probably have the even shorter C++ version. At times I wish they went faster but at least MS is steadily chipping away at language verbosity in C#; unlike Sun/whOracle who seem to revel in Java's bloatyness. :doh:

                                  Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                                  K Offline
                                  K Offline
                                  Kirk 10389821
                                  wrote on last edited by
                                  #38

                                  I would Java.Lang.Objection.RaiseSpecificObjection.Object to that slam on Java being wordy! :-)

                                  1 Reply Last reply
                                  0
                                  • M Munchies_Matt

                                    I always use your second style here, all function calls are broken up with one param per line, all neatly lined up. (I am so anal about code tidyness :) )

                                    K Offline
                                    K Offline
                                    Kirk 10389821
                                    wrote on last edited by
                                    #39

                                    We actually align the = signs as well. It makes it MUCH easier to read the 2 columns of assignment! (so you shouldn't feel overly anal about it)

                                    1 Reply Last reply
                                    0
                                    • J Jacquers

                                      Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

                                      K Offline
                                      K Offline
                                      Kirk 10389821
                                      wrote on last edited by
                                      #40

                                      I always hated that style of indent = { indent. When I learned C it was:

                                      void main()
                                      {
                                      IndentedCodeHere;
                                      }

                                      I cringed when I saw:

                                      void main()
                                      {
                                      CodeHere;
                                      }

                                      as if that made it easier to read. Then JavaScript came in

                                      function main() {
                                      Codehere;
                                      }

                                      Which I can live with, and I get. But it just seems wrong. It is VERY hard to match the braces visually, but the editors do it for me... But indentation matters.

                                      1 Reply Last reply
                                      0
                                      • J Jacquers

                                        Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";

                                        J Offline
                                        J Offline
                                        jfren484
                                        wrote on last edited by
                                        #41

                                        Am I not seeing what you're seeing? I don't see ANY indentation there. FWIW, I prefer this indentation:

                                        var student2 = new StudentName
                                        {
                                        FirstName = "Craig",
                                        LastName = "Playstead",
                                        };

                                        M 1 Reply Last reply
                                        0
                                        • J jfren484

                                          Am I not seeing what you're seeing? I don't see ANY indentation there. FWIW, I prefer this indentation:

                                          var student2 = new StudentName
                                          {
                                          FirstName = "Craig",
                                          LastName = "Playstead",
                                          };

                                          M Offline
                                          M Offline
                                          Middle Manager
                                          wrote on last edited by
                                          #42

                                          Just a personal thing but I hate code visually floating out to the right. I get that it lines up with the "new" but I find that disturbing, like I need to go in and fix it OCD style. I see the same thing with some default SQL formatters (like from RedGate). First thing I go to customize.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups