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.
  • 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
                  • H Henry Minute

                    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 Offline
                    W Offline
                    wout de zeeuw
                    wrote on last edited by
                    #27

                    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 1 Reply Last reply
                    0
                    • 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
                                          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