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. The use of 'var'

The use of 'var'

Scheduled Pinned Locked Moved C#
questiondiscussion
10 Posts 8 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.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    Since the var keyword has been introduced for anonymous types I have noticed more and more code examples (and seen in my work) people using var rather than a defined type. To my mind var should only be used when an anonymous type is being used and only then not instead of a defined type. What are peoples thoughts? are you seeing a rise in the use of var?

    J L G P 4 Replies Last reply
    0
    • N Nathan Gloyn

      Since the var keyword has been introduced for anonymous types I have noticed more and more code examples (and seen in my work) people using var rather than a defined type. To my mind var should only be used when an anonymous type is being used and only then not instead of a defined type. What are peoples thoughts? are you seeing a rise in the use of var?

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      If you give a novice programmer something to abuse, they will abuse it. Its about the first rule of computer programming. The only place I have used var so far is in the return from a LINQ query, I see no reason to use it extensively. So in short, I agree with you.

      S N 2 Replies Last reply
      0
      • N Nathan Gloyn

        Since the var keyword has been introduced for anonymous types I have noticed more and more code examples (and seen in my work) people using var rather than a defined type. To my mind var should only be used when an anonymous type is being used and only then not instead of a defined type. What are peoples thoughts? are you seeing a rise in the use of var?

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

        Well, I didn't declare a lot of "object x =" in .NET 2.0, doubt that I have more uses for the var-keyword. On the other hand, Resharper keeps reminding me that I should use "var" instead of the type-safe declaration. Ehr.. :doh: Dunno why actually :)

        I are troll :)

        R 1 Reply Last reply
        0
        • J J4amieC

          If you give a novice programmer something to abuse, they will abuse it. Its about the first rule of computer programming. The only place I have used var so far is in the return from a LINQ query, I see no reason to use it extensively. So in short, I agree with you.

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          I absolutely agree with you. As I see it, every developer must be aware what he/she is doing. He has to know about the types etc. he is using etc. Using a paradigma like "Uh I don't know what the return type of this method is, but hey who cares, the compiler will do it for me" is not the way I agree with. So in my opinion the excessive use of "var" makes the code difficult to read, you always have to search for the last assignment... As far as I know, var was introduced only for the use with LINQ, but as you already said, it will be abused... Regards Sebastian P.S.: Had a big discussion here last week, if we should use it throughout the code or not...

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          1 Reply Last reply
          0
          • L Lost User

            Well, I didn't declare a lot of "object x =" in .NET 2.0, doubt that I have more uses for the var-keyword. On the other hand, Resharper keeps reminding me that I should use "var" instead of the type-safe declaration. Ehr.. :doh: Dunno why actually :)

            I are troll :)

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            Eddy Vluggen wrote:

            Resharper keeps reminding me that I should use "var" instead of the type-safe declaration

            I know! Well, they've got it wrong. I hate the use of this subversive keyword. Fair dos for LINQ but no place elsewhere. Presumably you can tell Resharper not to do that, but I can't be bothered to wade through all the options pages. It does alsorts of things I don't care for - breaking my lines up, trying to use 'object initializers' to squeeze everything onto the construction line, putting space between casts etc. It should chill out a bit.

            Regards, Rob Philpott.

            1 Reply Last reply
            0
            • N Nathan Gloyn

              Since the var keyword has been introduced for anonymous types I have noticed more and more code examples (and seen in my work) people using var rather than a defined type. To my mind var should only be used when an anonymous type is being used and only then not instead of a defined type. What are peoples thoughts? are you seeing a rise in the use of var?

              G Offline
              G Offline
              Gideon Engelberth
              wrote on last edited by
              #6

              Since you're asking for opinions, here's mine. I would not be opposed to seeing var when you are declaring a new something (especially when the type name is long). For example:

              var cache = new Dictionary<string, LongTypeNameIWantToTypeJustOnce>();

              as opposed to

              Dictionary<string, LongTypeNameIWantToTypeJustOnce> cache = new Dictionary<string, LongTypeNameIWantToTypeJustOnce>();

              which I either have to break up into multiple lines or have one really long line. Even if the line is not too long, the second way is still noisier than the first. For the record, this is one of the things I like about the VB syntax (only needing to write the type once when declaring and "new"-ing a variable at the same time)

              1 Reply Last reply
              0
              • N Nathan Gloyn

                Since the var keyword has been introduced for anonymous types I have noticed more and more code examples (and seen in my work) people using var rather than a defined type. To my mind var should only be used when an anonymous type is being used and only then not instead of a defined type. What are peoples thoughts? are you seeing a rise in the use of var?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I agree, it is being abused. I never use var, and as I also never use Linq, I don't expect to.

                R 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I agree, it is being abused. I never use var, and as I also never use Linq, I don't expect to.

                  R Offline
                  R Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  The LINQ to XML stuff makes handling XML files a breeze.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  P 1 Reply Last reply
                  0
                  • R realJSOP

                    The LINQ to XML stuff makes handling XML files a breeze.

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    I just use an XmlDocument. I was doing some more XSL last night. Maybe you have a Linq way of doing this... In my XML file I have a processing instruction to indicate the XSL file and stylesheet to use: <?xml-stylesheet type="text/xsl" href="mystylesheets.xsl#somestylesheet"?> Unfortunately, those aren't Attributes, they are in the Value! I've been using a Regular Expression to get this information:

                    private static readonly System.Text.RegularExpressions.Regex HrefReg =
                    new System.Text.RegularExpressions.Regex
                    (
                    "href\\s*=\\s*(?'quot'[\"'])(?'href'.*?)((?'sep'#)(?'id'.*?))?\\k'quot'"
                    ) ;

                    Do you have a Linqish way to do that?

                    1 Reply Last reply
                    0
                    • J J4amieC

                      If you give a novice programmer something to abuse, they will abuse it. Its about the first rule of computer programming. The only place I have used var so far is in the return from a LINQ query, I see no reason to use it extensively. So in short, I agree with you.

                      N Offline
                      N Offline
                      Nathan Gloyn
                      wrote on last edited by
                      #10

                      What has been worrying me is more senior experienced programmers starting to do it. Brought back horrible memories of VB6 variant

                      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