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.
  • W wout de zeeuw

    Well, at least in VS it is... as soon as you type "new " the autocompletion will happen. So you'll type "List" just once, whereas when using var you have to type the extra var.

    Wout

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

    OK. Now I understand. :)

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

    1 Reply Last reply
    0
    • P Pete OHanlon

      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 Offline
      M Offline
      Mustafa Ismail Mustafa
      wrote on last edited by
      #29

      That would be a mess...

      If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

      1 Reply Last reply
      0
      • W wout de zeeuw

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

        No.

        W 1 Reply Last reply
        0
        • S Stuart Dootson

          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 Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #31

          Stuart Dootson wrote:

          Does the 'crusty old' relate to you or C++?

          It works either way. :-D

          Stuart Dootson wrote:

          (((crusty old) C++) guy)!

          Do we allow Lisp in here now? ;)

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

          1 Reply Last reply
          0
          • P PIEBALDconsult

            No.

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

            Yes.

            Wout

            C 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

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #33

              Marc Clifton wrote:

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

              Well if both the variable and the factory method are named properly, I don't think there'd be any confusion here. If they are not, then using var would be the least of the issues here.

              Regards, Nish


              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
              My latest book : C++/CLI in Action / Amazon.com link

              1 Reply Last reply
              0
              • W wout de zeeuw

                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

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

                no, the point was more to be able to have anonymous types and also in foreach and other like constructs where the type info can be combersomely unwieldy to have something more readable and understandable.

                W 1 Reply Last reply
                0
                • I Ian Shlasko

                  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 Offline
                  T Offline
                  TheGreatAndPowerfulOz
                  wrote on last edited by
                  #35

                  yes, true, but you're not always using your own "reasonable" code...

                  1 Reply Last reply
                  0
                  • I Ian Shlasko

                    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)

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #36

                    Then you lose any constructors the base type might have had. Although it's annoying you have to fully qualify the namespaces, you could also do this:

                    using TooLong = System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int>>>>;

                    Visual Studio is an excellent GUIIDE.

                    1 Reply Last reply
                    0
                    • M Marc Clifton

                      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 Offline
                      R Offline
                      Roger Wright
                      wrote on last edited by
                      #37

                      But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?

                      "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                      J S 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)

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

                        List<string> myList = List<string>.ctor() ; Much more orthogonal. :-D

                        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
                          Plamen Dragiyski
                          wrote on last edited by
                          #39

                          //C/C++:
                          void *foo = (void*)new Foo();
                          //What is foo?
                          foo = (void*)myClass.createFoo();
                          //And now?
                          foo = (void*)&any_type_variable;
                          //now?
                          foo = (void*)&foo;
                          //O.o

                          Seems nice to me :P

                          1 Reply Last reply
                          0
                          • R Roger Wright

                            But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?

                            "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                            J Offline
                            J Offline
                            Jorgen Sigvardsson
                            wrote on last edited by
                            #40

                            let var Foo2 = old FooObject; of course... (Seriously, no. The compiler substitutes the var keyword with the real type at compile time. It's not a dynamic type.)

                            -- Kein Mitleid Für Die Mehrheit

                            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

                              D Offline
                              D Offline
                              dazfuller
                              wrote on last edited by
                              #41

                              I hate it because you can't tell at a glance what the type is, okay so its alright for anonymous types where you don't know but otherwise it's just a train-wreck. I still think that the following is cleaner, easier to read and less prone to human error: List foo = new List(); and IFoo foo = factory.CreateAFoo()

                              1 Reply Last reply
                              0
                              • C Chris Losinger

                                Marc Clifton wrote:

                                var foo = factory.CreateAFoo()

                                into every language a little void * must fall.

                                image processing toolkits | batch image processing

                                A Offline
                                A Offline
                                Adar Wesley
                                wrote on last edited by
                                #42

                                Chris Losinger wrote:

                                Marc Clifton wrote: var foo = factory.CreateAFoo() into every language a little void * must fall.

                                In the above case, foo is NOT void *. It is a strongly typed variable of the type factory.CreateAFoo() returns. For instance:

                                var str = "This is a string variable";
                                str = 5; // <-- Compile error

                                Incidently, I love to use var. During development you change the type of a variable by changing the initialization code and not have to go also and fix the variable declared type. So if I have in my code the above mentioned function declared as so:

                                internal Foo CreateAFoo()
                                {
                                // create and return a Foo
                                }

                                And somewhere else:

                                var foo = factory.CreateAFoo()

                                Then, during the developement process, I think: "let's change my work from Foo to IFoo to introduce different implementations". All I have to do is change the signature of CreateAFoo() to:

                                internal IFoo CreateAFoo()
                                {
                                // create and return a Foo
                                }

                                That's it, all done. Don't need to wory about changing all the variables all over the code that hold a Foo reference. They automatically become IFoo references. --- Adar Wesley

                                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

                                  W Offline
                                  W Offline
                                  Wenff
                                  wrote on last edited by
                                  #43

                                  Talk about lazy! Is t really that hard type ()? ;P

                                  1 Reply Last reply
                                  0
                                  • R Roger Wright

                                    But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?

                                    "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                                    S Offline
                                    S Offline
                                    smcnulty2000
                                    wrote on last edited by
                                    #44

                                    Roger Wright wrote:

                                    var Foo2 = old FooObject;

                                    There's no foo like an old foo. So, clearly not.

                                    _____________________________ There is no I in team. But there is meat in there.

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

                                      K Offline
                                      K Offline
                                      K v S
                                      wrote on last edited by
                                      #45

                                      :laugh: try COBOL

                                      1 Reply Last reply
                                      0
                                      • T TheGreatAndPowerfulOz

                                        no, the point was more to be able to have anonymous types and also in foreach and other like constructs where the type info can be combersomely unwieldy to have something more readable and understandable.

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

                                        It only saves some typing, other than that it doesn't offer anything functional. I can make my own custom classes to contain my query/filter results, which will always be more readable than a non-descriptive var.

                                        Wout

                                        1 Reply Last reply
                                        0
                                        • J Judah Gabriel Himango

                                          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 Offline
                                          C Offline
                                          codemunkeh
                                          wrote on last edited by
                                          #47

                                          Why not just make all your own custom types 7 characters long; and build wrappers around the base classes to make them 7 characters long too?

                                          QString bork = string.Empty
                                          QQFloat FHeight = 12.0F

                                          Really, what I normally do is group the declarations at the top of a class inside a #region Declarations then shrink it when not needed.


                                          Ninja (the Nerd)
                                          Confused? You will be...

                                          J 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