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

            Yes.

            Wout

            C Offline
            C Offline
            codemunkeh
            wrote on last edited by
            #48

            UnstableClassInDevelopment.UnStableType.UnderlyingValue obj = new UnstableClassInDevelopment.UnStableType.UnderlyingValue();

            Now, if that underlying value changes type; everything breaks. With var, you don't have to do any search/replace and it works with any reasonable change. Yes, stupid example but I don't even use the thing.


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

            W 1 Reply Last reply
            0
            • C codemunkeh

              UnstableClassInDevelopment.UnStableType.UnderlyingValue obj = new UnstableClassInDevelopment.UnStableType.UnderlyingValue();

              Now, if that underlying value changes type; everything breaks. With var, you don't have to do any search/replace and it works with any reasonable change. Yes, stupid example but I don't even use the thing.


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

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

              You can just rename the type with VS refactoring couldn't you? And if some third party owns that code and changes it, I would like everything to break, so I can see what the change was exactly. Silently accepting changes is very dangerous.

              Wout

              P 1 Reply 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)

                J Offline
                J Offline
                James Curran
                wrote on last edited by
                #50

                var foo = new MyFoo(); //clearly creates a new MyFoo. var foo = MyFoo(); //create a new MyFoo or call local method MyFoo()? var foo = MyFoo; //create a new MyFoo or assign local field MyFoo?

                Truth, James

                1 Reply Last reply
                0
                • S smcnulty2000

                  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 Offline
                  R Offline
                  Richard Jones
                  wrote on last edited by
                  #51

                  I pity the foo! - Mr <T>

                  "The activity of 'debugging', or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed." - "Datamation", January 15, 1984

                  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

                    J Offline
                    J Offline
                    James Curran
                    wrote on last edited by
                    #52

                    >> why on earth C# (or Java) need keyword new in the first place? It is completely redundant. It's not for the compiler, but for human readers. The goal was that the programmer's intend is always clear. For more examples, there's no reason why classes need to be marked abstract; or that virtual methods be marked "new" or "override"; or that we have both "ref" and "out" to mark parameters. none of those affects the generated IL at all.

                    Truth, James

                    P 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

                      L Offline
                      L Offline
                      leonej_dt
                      wrote on last edited by
                      #53

                      I write new List() even in C++. The constructor is a function, even if it's implicit.

                      If you can play The Dance of Eternity (Dream Theater), then we shall make a band.

                      1 Reply Last reply
                      0
                      • R Richard Jones

                        I pity the foo! - Mr <T>

                        "The activity of 'debugging', or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed." - "Datamation", January 15, 1984

                        M Offline
                        M Offline
                        MiddleTommy
                        wrote on last edited by
                        #54

                        I pity the foo! Now eat my cereal - Pee Wee Herman acting like Mr

                        1 Reply Last reply
                        0
                        • C codemunkeh

                          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 Offline
                          J Offline
                          Judah Gabriel Himango
                          wrote on last edited by
                          #55

                          Ninja-the-Nerd wrote:

                          Why not just make all your own custom types 7 characters long;

                          Length isn't the issue.

                          Ninja-the-Nerd wrote:

                          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.

                          Me too. But var doesn't work for fields in a class. Only works for locals.

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

                          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

                            G Offline
                            G Offline
                            grgran
                            wrote on last edited by
                            #56

                            There is another side to this. That the example you gave of var foo = factory.CreateAFoo() and lets assume that CreateAFoo returns a IFoo interface. Now assume that at some point you add a new method to the interface can create a IFoo2 interface. You don't have to change your code. I like var ... less typing and if I can't easily figure out the type, then that tells me that the code needs a comment :-)

                            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

                              J Offline
                              J Offline
                              JasonPSage
                              wrote on last edited by
                              #57

                              Perhaps I'm daft, but you do need to indicate a consturctor - so as to call the default constructor and possibly others (with parameters or different name). However I do agree that var foo = some-type could cause default constructor invokation/object creation while the second example from the factory would work without new because you're in theory returning a pointer to an instanced object. Ultimately New does seem redundant to me, but you still need a clear semantic/syntax to be able to indicate default and potentially other constructor invocation for class/object creation --Jason

                              Know way too many languages... master of none!

                              1 Reply Last reply
                              0
                              • J James Curran

                                >> why on earth C# (or Java) need keyword new in the first place? It is completely redundant. It's not for the compiler, but for human readers. The goal was that the programmer's intend is always clear. For more examples, there's no reason why classes need to be marked abstract; or that virtual methods be marked "new" or "override"; or that we have both "ref" and "out" to mark parameters. none of those affects the generated IL at all.

                                Truth, James

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

                                Keywords like abstract not only show the intent, but enforce it as well.

                                James Curran wrote:

                                none of those affects the generated IL at all

                                Then how are they enforced when you refer to something in an assembly?

                                1 Reply Last reply
                                0
                                • W wout de zeeuw

                                  You can just rename the type with VS refactoring couldn't you? And if some third party owns that code and changes it, I would like everything to break, so I can see what the change was exactly. Silently accepting changes is very dangerous.

                                  Wout

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

                                  Hear hear!

                                  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

                                    K Offline
                                    K Offline
                                    Kevin McFarlane
                                    wrote on last edited by
                                    #60

                                    Nemanja Trifunovic wrote:

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

                                    Hey Nemanja I've been thinking that very thought ever since Java and then C# came along but I'd never voiced it before! :)

                                    Kevin

                                    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

                                      K Offline
                                      K Offline
                                      Kevin McFarlane
                                      wrote on last edited by
                                      #61

                                      Not really used var in anger. But the first will probably be my usage pattern going forward.

                                      Kevin

                                      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

                                        K Offline
                                        K Offline
                                        Kevin McFarlane
                                        wrote on last edited by
                                        #62

                                        Marc Clifton wrote:

                                        Just be glad we don't have to use the "Let" keyword (though, in some functional languages, it's baaaaack!)

                                        e.g., in F#. Let is just var (well, actually it's val if you compare it to Scala which has var and val - var for mutables, val for immutables).

                                        Kevin

                                        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

                                          J Offline
                                          J Offline
                                          James Lonero
                                          wrote on last edited by
                                          #63

                                          Why is there a "new"? This is to keep Java and C# more in line with C++, and to help woo over more C++ programmers. It does, generally, the same thing as in C++, therefore something that the C++ programmers need not learn. Yes, us old C++ programmers need something familiar to help win our hearts. But, you don't need to delete what you new'ed. That gets gc'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