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. var

var

Scheduled Pinned Locked Moved The Lounge
csharpcomhelptutorial
64 Posts 40 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

    Will work for food. Interacx

    I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

    N J I T C 18 Replies Last reply
    0
    • M Marc Clifton

      So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

      Will work for food. Interacx

      I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Marc Clifton wrote:

      var foo = factory.CreateAFoo()

      Meh, in your example foo is either Foo or IFoo. On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

      Programming Blog utf8-cpp

      M C C J K 7 Replies Last reply
      0
      • M Marc Clifton

        So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

        Will work for food. Interacx

        I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        My opinion: I'm kind of liking var. Everywhere. While I might not be able to tell the type at only a quick glance, as you say in your post, it makes the code much cleaner and elegant, IMO, as the variable declarations all stand out as a single group.

        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

        C 1 Reply Last reply
        0
        • N Nemanja Trifunovic

          Marc Clifton wrote:

          var foo = factory.CreateAFoo()

          Meh, in your example foo is either Foo or IFoo. On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

          Programming Blog utf8-cpp

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          Nemanja Trifunovic wrote:

          Meh, in your example foo is either Foo or IFoo.

          Agreed. The example was bad, but you know what I meant. :)

          Nemanja Trifunovic wrote:

          why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

          Because it gives everyone a warm fuzzy feeling that something important is happening. Just be glad we don't have to use the "Let" keyword (though, in some functional languages, it's baaaaack!) Marc

          Will work for food. Interacx

          I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

          R K 2 Replies Last reply
          0
          • M Marc Clifton

            So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

            Will work for food. Interacx

            I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

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

            Agreed... Only two places I'll use var... 1) In initializations like you described, when the type is right there anyway 2) Linq anonymous types

            Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

            1 Reply Last reply
            0
            • M Marc Clifton

              So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

              Will work for food. Interacx

              I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

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

              you have a point. I've taken to only using var where the type is obvious.

              1 Reply Last reply
              0
              • M Marc Clifton

                So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                Will work for food. Interacx

                I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                Marc Clifton wrote:

                var foo = factory.CreateAFoo()

                into every language a little void * must fall.

                image processing toolkits | batch image processing

                A 1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  Marc Clifton wrote:

                  var foo = factory.CreateAFoo()

                  Meh, in your example foo is either Foo or IFoo. On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

                  Programming Blog utf8-cpp

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Because Bill (Jim's mate) keep calling instances capitalized... :rolleyes:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                    Will work for food. Interacx

                    I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P

                    --Mike-- Dunder-Mifflin, this is Pam

                    B S W L 4 Replies Last reply
                    0
                    • M Michael Dunn

                      It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P

                      --Mike-- Dunder-Mifflin, this is Pam

                      B Offline
                      B Offline
                      Brady Kelly
                      wrote on last edited by
                      #10

                      The other day, I was staring at the screen nodding off, and I typed "var ls = List", and it looked so right. I ascended and send a messenger down to the C# 5 team. Just wait and see. :)

                      I have been trying for weeks to get this little site indexed. If you wonder what it is, or would like some informal accommodation for the 2010 World Cup, please click on this link for Rhino Cottages.

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                        Will work for food. Interacx

                        I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

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

                        Only use it where it's needed.

                        Marc Clifton wrote:

                        it's obvious what foo is

                        Then don't use var.

                        Marc Clifton wrote:

                        That's where I despise seeing a "var"!

                        And yet it makes a little more sense there.

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                          Will work for food. Interacx

                          I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                          W Offline
                          W Offline
                          wout de zeeuw
                          wrote on last edited by
                          #12

                          Marc Clifton wrote:

                          var foo = new List();

                          This is actually more typing than

                          List foo = new List();

                          so there it would be kinda useless too.

                          Wout

                          P H 2 Replies Last reply
                          0
                          • M Marc Clifton

                            So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                            Will work for food. Interacx

                            I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                            R Offline
                            R Offline
                            Robert Surtees
                            wrote on last edited by
                            #13

                            Just make all undeclared variables vars and do away with the keyword altogether. ;)

                            L 1 Reply Last reply
                            0
                            • M Marc Clifton

                              So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                              Will work for food. Interacx

                              I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                              S Offline
                              S Offline
                              Shog9 0
                              wrote on last edited by
                              #14

                              Marc Clifton wrote:

                              var foo = factory.CreateAFoo() That's where I despise seeing a "var"!

                              That's where I despise seeing factories... ;) Think about it: new List() obviously creates an instance of List, hence the utility of var. You'd expect CreateAFoo() to create an instance of something named Foo, thereby preserving the utility of var - since the author instead chose to return a list without indicating this anywhere in the method name, you're trapped, trapped like a rat, between the choice to write code that is verbose and code that is unclear. The problem hardly begins with var either; your code becomes similarly opaque if you pass the result of the Create... call directly as a parameter to another method.

                              1 Reply Last reply
                              0
                              • R Robert Surtees

                                Just make all undeclared variables vars and do away with the keyword altogether. ;)

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                or use some other language, such as PHP. More freedom, more joy. :laugh:

                                Luc Pattyn


                                Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


                                Local announcement (Antwerp region): Lange Wapper? Neen!


                                1 Reply Last reply
                                0
                                • M Michael Dunn

                                  It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P

                                  --Mike-- Dunder-Mifflin, this is Pam

                                  S Offline
                                  S Offline
                                  Stuart Dootson
                                  wrote on last edited by
                                  #16

                                  Does the 'crusty old' relate to you or C++? ;P Use parentheses to disambiguate, young man - either (crusty old) (C++ guy) or (((crusty old) C++) guy)!

                                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                  M 1 Reply Last reply
                                  0
                                  • N Nemanja Trifunovic

                                    Marc Clifton wrote:

                                    var foo = factory.CreateAFoo()

                                    Meh, in your example foo is either Foo or IFoo. On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

                                    Programming Blog utf8-cpp

                                    C Offline
                                    C Offline
                                    Christian Graus
                                    wrote on last edited by
                                    #17

                                    Nemanja Trifunovic wrote:

                                    On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

                                    So objects can be null. So you can control where an object is declared ( as in, if they are a member, etc ), and also control when you pay the cost of creating them.

                                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                                    I 1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                                      Will work for food. Interacx

                                      I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                                      A Offline
                                      A Offline
                                      Adam Maras
                                      wrote on last edited by
                                      #18

                                      What if CreateAFoo() returns something ridiculous like IEnumerable<Dictionary<int, Dictionary<int, List<int>>>>?

                                      Adam Maras | Software Developer Microsoft Certified Professional Developer

                                      I 1 Reply Last reply
                                      0
                                      • C Christian Graus

                                        Nemanja Trifunovic wrote:

                                        On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.

                                        So objects can be null. So you can control where an object is declared ( as in, if they are a member, etc ), and also control when you pay the cost of creating them.

                                        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

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

                                        I kind of like the "new" keyword, but technically it shouldn't be needed, unless I'm missing something. Just playing devil's advocate here...

                                        With: List<string> myList = new List<string>();
                                        Without: List<string> myList = List<string>();

                                        The parentheses would be enough to indicate that you're calling a constructor... I do think, though, that the "new" keyword keeps things clearer. There could be issues with functions named the same as classes, but that could technically be resolved with absolute references.

                                        Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

                                        C P J 3 Replies Last reply
                                        0
                                        • M Marc Clifton

                                          So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with: var foo = new List(); as an example, because it's obvious what foo is. What I really hate is something like this: var foo = factory.CreateAFoo() That's where I despise seeing a "var"! Marc

                                          Will work for food. Interacx

                                          I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

                                          P Offline
                                          P Offline
                                          Pete OHanlon
                                          wrote on last edited by
                                          #20

                                          Marc Clifton wrote:

                                          var foo = factory.CreateAFoo() That's where I despise seeing a "var"!

                                          So what you have here is var foo = factory.CreateAPoo();

                                          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                          My blog | My articles | MoXAML PowerToys | Onyx

                                          M 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