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 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
                • 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
                  #14

                  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 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
                    #15

                    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
                    • 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
                              • A Adam Maras

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

                                Adam Maras | Software Developer Microsoft Certified Professional Developer

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

                                public class MyRidiculousClass : IEnumerable<Dictionary<int, Dictionary<int, List<int>>>>
                                {
                                }

                                Now CreateAFoo() can return something legible :)

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

                                T A 2 Replies Last reply
                                0
                                • W wout de zeeuw

                                  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 Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #22

                                  var wasn't created to reduce keystrokes, and should not be used as such. Developers should strive to type more keystrokes, not fewer.

                                  W K 2 Replies Last reply
                                  0
                                  • I Ian Shlasko

                                    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 Offline
                                    C Offline
                                    Christian Graus
                                    wrote on last edited by
                                    #23

                                    OK, I guess that would work. If the method was not generic, what if you had an object called List, AND a method called List in scope that returns a List ?

                                    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
                                    • P PIEBALDconsult

                                      var wasn't created to reduce keystrokes, and should not be used as such. Developers should strive to type more keystrokes, not fewer.

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

                                      Huh? Isn't the point of var and anonymous types less typing? Otherwise one would type out all these types explicitly. Other than amount of typing I see no advantage in var.

                                      Wout

                                      P T 2 Replies Last reply
                                      0
                                      • W wout de zeeuw

                                        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

                                        H Offline
                                        H Offline
                                        Henry Minute
                                        wrote on last edited by
                                        #25

                                        Parding? I've looked and looked at your post and according to my count your code has one more letter than Marc's. So how can his be more typing than yourn?

                                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                        W 1 Reply Last reply
                                        0
                                        • C Christian Graus

                                          OK, I guess that would work. If the method was not generic, what if you had an object called List, AND a method called List in scope that returns a List ?

                                          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
                                          #26

                                          Then there would have to be a standard rule to handle it, the same way the compiler handles ambiguous methods... Local gets priority over external, same assembly gets priority over referenced assembly... Anything that can't be resolved throws an error and forces you to prefix the class or namespace. (But again, just looking at the other side here - I think new is a good thing)

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

                                          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