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. General Programming
  3. C#
  4. Implicit Variables

Implicit Variables

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
5 Posts 3 Posters 0 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.
  • D Offline
    D Offline
    Darrall
    wrote on last edited by
    #1

    I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.

    var viewModel = new StoreIndexViewModel {
    NumberOfGenres = genres.Count(),
    Genres = genres};

    I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall

    A R 2 Replies Last reply
    0
    • D Darrall

      I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.

      var viewModel = new StoreIndexViewModel {
      NumberOfGenres = genres.Count(),
      Genres = genres};

      I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      This feature of C# is known as "Object Initializers". Read about it here or on msdn.

      D 1 Reply Last reply
      0
      • A Abhinav S

        This feature of C# is known as "Object Initializers". Read about it here or on msdn.

        D Offline
        D Offline
        Darrall
        wrote on last edited by
        #3

        Thanks :)

        1 Reply Last reply
        0
        • D Darrall

          I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.

          var viewModel = new StoreIndexViewModel {
          NumberOfGenres = genres.Count(),
          Genres = genres};

          I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall

          R Offline
          R Offline
          riced
          wrote on last edited by
          #4

          You can make the code more readable (though that's subjective). E.g

          var x = new Dictionary<string, List<person>>();

          instead of

          Dictionary<string, List<person>> x = new Dictionary<string, List<person>>();

          You can use them in LINQ queries where the result type is not obvious - the compiler will get it right. You can find out the type by hovering over the var in VS - it may not be what you think. Worth experimenting with. This is one of those topics that cause 'religious' debates or flame wars - though that seems to have died down. Some people say 'Never' others 'Always'. :-D

          Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

          D 1 Reply Last reply
          0
          • R riced

            You can make the code more readable (though that's subjective). E.g

            var x = new Dictionary<string, List<person>>();

            instead of

            Dictionary<string, List<person>> x = new Dictionary<string, List<person>>();

            You can use them in LINQ queries where the result type is not obvious - the compiler will get it right. You can find out the type by hovering over the var in VS - it may not be what you think. Worth experimenting with. This is one of those topics that cause 'religious' debates or flame wars - though that seems to have died down. Some people say 'Never' others 'Always'. :-D

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            D Offline
            D Offline
            Darrall
            wrote on last edited by
            #5

            Thanks...that makes sense :)

            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