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 3 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

    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
        • 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
          CoolDadTx
          wrote on last edited by
          #64

          How do you know it is a Foo? Do you really trust method names that much? If you do then the least of your worries should be what type foo actually is. Here's just a few examples of a Get/CreateX where X dosn't mean anything in regards to the return type: GetParent, GetOwner, GetDesignModeState, CreateHandle, CreateControlsInstance, etc. So given the original statement you can infer nothing about the type of foo. BTW new isn't redundant in all cases. For example what should be the result of this: class MyClass { Type Type ( int default = 1 ) { ... } void Initialize ( ) { Type temp = Type(); } } Now you might say that the default parameter is not valid in C# but it is as of v4. Since C# (and Java) are based upon C++ they follow many of the same rules partially for consistency and partially for support of similar features. Defaults weren't in the original spec for ideological reasons but to make an inconsistent change to prevent their usage would be overkill. I do agree however that it would be nice to eliminate the need for new in the few cases where its usage is redundant such as when inlining a temp variable in which the concrete type is known.

          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