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. General Programming
  3. C#
  4. Visual Studio - how to change "var" to corresponding class

Visual Studio - how to change "var" to corresponding class

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelptutorialquestion
19 Posts 9 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.
  • B BillWoodruff

    If you use ReSharper, there is an option to switch to explicit Type declaration from 'var via either a left-margin click on an icon, or by keyboard alt-enter.

    «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #4

    Really should be part of VS by now... :sigh:

    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    1 Reply Last reply
    0
    • B BillWoodruff

      If you use ReSharper, there is an option to switch to explicit Type declaration from 'var via either a left-margin click on an icon, or by keyboard alt-enter.

      «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #5

      ReSharper is the demon responsible for most of this var nonsense!

      // Can you guess what VS plug-in I use?
      var customers = ctx.Customers.Where(c => c.HasOrders);
      var customer = customers.FirstOrDefault();
      if (customer != null)
      {
      var orderCount = customer.Orders.Count;
      var value = customer.Orders.Sum(o => o.Total);
      var postage = ctx.Postage.Where(p => P.Country == customer.Country);
      var valuePlusPostage = value += postage;
      var tax = financeHelper.TaxRate;
      }

      M Richard DeemingR 2 Replies Last reply
      0
      • F F ES Sitecore

        ReSharper is the demon responsible for most of this var nonsense!

        // Can you guess what VS plug-in I use?
        var customers = ctx.Customers.Where(c => c.HasOrders);
        var customer = customers.FirstOrDefault();
        if (customer != null)
        {
        var orderCount = customer.Orders.Count;
        var value = customer.Orders.Sum(o => o.Total);
        var postage = ctx.Postage.Where(p => P.Country == customer.Country);
        var valuePlusPostage = value += postage;
        var tax = financeHelper.TaxRate;
        }

        M Offline
        M Offline
        Maarten1977
        wrote on last edited by
        #6

        But if you don't like the option that changes a fully-specified type to 'var', why are you using it? I'm assuming here that Resharper doesn't do that by itself, or if it does that it is a 'feature' that can be turned off. (No, I am not using Resharper myself, never have) (Yes, I do prefer 'var': it saves keystrokes; intellisense provides me with the name of type if I need it)

        F 1 Reply Last reply
        0
        • M Maarten1977

          But if you don't like the option that changes a fully-specified type to 'var', why are you using it? I'm assuming here that Resharper doesn't do that by itself, or if it does that it is a 'feature' that can be turned off. (No, I am not using Resharper myself, never have) (Yes, I do prefer 'var': it saves keystrokes; intellisense provides me with the name of type if I need it)

          F Offline
          F Offline
          F ES Sitecore
          wrote on last edited by
          #7

          I don't use resharper myself but most of my colleagues do. In its default configuration it flags a warning on any use of explicit types, suggesting you use var instead. Yes it can be turned off, but no-one does. And yes it can also be ignored, but people would rather just use var to get rid of the wiggly line (see previous comment).

          L M 2 Replies Last reply
          0
          • F F ES Sitecore

            ReSharper is the demon responsible for most of this var nonsense!

            // Can you guess what VS plug-in I use?
            var customers = ctx.Customers.Where(c => c.HasOrders);
            var customer = customers.FirstOrDefault();
            if (customer != null)
            {
            var orderCount = customer.Orders.Count;
            var value = customer.Orders.Sum(o => o.Total);
            var postage = ctx.Postage.Where(p => P.Country == customer.Country);
            var valuePlusPostage = value += postage;
            var tax = financeHelper.TaxRate;
            }

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #8

            And it's not even picked up some of the obvious things! :D

            // Where followed by FirstOrDefault or SingleOrDefault can be replaced with an overload of the second function:
            var customer = ctx.Customers.FirstOrDefault(c => c.HasOrders);
            ...
            // This is an IQueryable<T> - you can't add it to a number in the line below:
            var postage = ctx.Postage.Where(p => P.Country == customer.Country);
            ...
            // Did you really mean to modify the value variable as well as the valuePlusPostage here?
            var valuePlusPostage = value += postage;


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • F F ES Sitecore

              I don't use resharper myself but most of my colleagues do. In its default configuration it flags a warning on any use of explicit types, suggesting you use var instead. Yes it can be turned off, but no-one does. And yes it can also be ignored, but people would rather just use var to get rid of the wiggly line (see previous comment).

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              F-ES Sitecore wrote:

              people would rather just use var

              They should be made to rewrite their code in COBOL. ;)

              1 Reply Last reply
              0
              • F F ES Sitecore

                I don't use resharper myself but most of my colleagues do. In its default configuration it flags a warning on any use of explicit types, suggesting you use var instead. Yes it can be turned off, but no-one does. And yes it can also be ignored, but people would rather just use var to get rid of the wiggly line (see previous comment).

                M Offline
                M Offline
                Maarten1977
                wrote on last edited by
                #10

                Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                P F R 5 Replies Last reply
                0
                • M Maarten1977

                  Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  You're doing a code review in Crucible and you see this statement:

                  var descendant = GetDescendant();

                  What is the type of descendant? Is it nullable? Surely Intellisense will tell me what the type is? Oh wait, I'm in a web browser now.

                  This space for rent

                  M 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    You're doing a code review in Crucible and you see this statement:

                    var descendant = GetDescendant();

                    What is the type of descendant? Is it nullable? Surely Intellisense will tell me what the type is? Oh wait, I'm in a web browser now.

                    This space for rent

                    M Offline
                    M Offline
                    Maarten1977
                    wrote on last edited by
                    #12

                    I implied we were talking about developers and var, not how it affects reviewers. But fair enough I guess. If you know what GetDescendant() does, then you'll know the return type (assuming here). If you don't know what the method does, then maybe that should be made clear first. What is more important, the method and what it does, or the specific technical return type? I'm not trying to convince you of anything, I'm just trying to understand your way of thinking.

                    P 1 Reply Last reply
                    0
                    • M Maarten1977

                      Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                      F Offline
                      F Offline
                      F ES Sitecore
                      wrote on last edited by
                      #13

                      Pretty much what Pete said, you can't discern the types simply by looking at them. Can I find them out? Yes. But I'd rather not have to find them out, I'd rather know by just looking at the code.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • M Maarten1977

                        I implied we were talking about developers and var, not how it affects reviewers. But fair enough I guess. If you know what GetDescendant() does, then you'll know the return type (assuming here). If you don't know what the method does, then maybe that should be made clear first. What is more important, the method and what it does, or the specific technical return type? I'm not trying to convince you of anything, I'm just trying to understand your way of thinking.

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #14

                        maarten_oosterhoff wrote:

                        not how it affects reviewers. But fair enough I guess.

                        Who does your code reviews if it's not developers?

                        maarten_oosterhoff wrote:

                        If you know what GetDescendant() does, then you'll know the return type (assuming here).

                        And what happens when you're reviewing a piece of code that calls a method that was written 5 years ago that you have never had cause to look into? You're introducing blockers for reviewers just because you can't be bothered to type the full type.

                        maarten_oosterhoff wrote:

                        What is more important, the method and what it does, or the specific technical return type?

                        If you don't know the return type, how can you tell if the person whose code you're reviewing is using the type properly or that it will even compile? You have to give people the context to help them.

                        This space for rent

                        1 Reply Last reply
                        0
                        • F F ES Sitecore

                          Pretty much what Pete said, you can't discern the types simply by looking at them. Can I find them out? Yes. But I'd rather not have to find them out, I'd rather know by just looking at the code.

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #15

                          F-ES Sitecore wrote:

                          you can't discern the types simply by looking at them

                          Agreed, for things like:

                          var frob = DoSomething();

                          But, for cases where the type is obvious, var cuts down on the clutter:

                          var myFoos = new Dictionary<string, Foo>();

                          // Vs:
                          // Dictionary<string, Foo> myFoos = new Dictionary<string, Foo>();

                          However, if for some reason you need the variable type to be an interface or a base type, var isn't a good option:

                          IDictionary<string, Foo> myFoos = new Dictionary<string, Foo>();

                          // Vs:
                          // var myFoos = (IDictionary<string, Foo>)new Dictionary<string, Foo>();

                          Like almost anything, var has sensible uses and no-so-sensible uses. It's up to the developer to decide which is which! :)


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          1 Reply Last reply
                          0
                          • M Maarten1977

                            Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                            R Offline
                            R Offline
                            realJSOP
                            wrote on last edited by
                            #16

                            var is a compromise my Microsoft that allows VB last-resorters be okay with using a more appropriate strongly-typed language instead of just conforming to a better paradigm than "it's type is whatever it wants to be". In essence, it is the "safe space" of the C# programming language, and is only intentionally used by flaming lib-tards that think all code should be equal.

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            1 Reply Last reply
                            0
                            • M Maarten1977

                              Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                              R Offline
                              R Offline
                              realJSOP
                              wrote on last edited by
                              #17

                              var is a Microsoft mechanism that allows VB last resorters to be okay with using as more appropriate strongly-typed language rather than comply with the actual paradigm of being a real programmer. In essence, it is the "safe space" of programming constructs and as such, is used only by flaming lib-tards that think all code should be equal in the eyes of the compiler. That's what's wrong with var.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                              1 Reply Last reply
                              0
                              • M Maarten1977

                                Ok, I'll bite. But I am not saying this or that, just interested in your opinion. Why wouldn't you use var? What's wrong with it?

                                R Offline
                                R Offline
                                realJSOP
                                wrote on last edited by
                                #18

                                var is a Microsoft mechanism that allows VB last-resorters to be okay with using as more appropriate strongly-typed language rather than comply with the actual paradigm of being a real programmer. In essence, it is the "safe space" of programming constructs and as such, is used only by flaming lib-tards that think all code should be equal in the eyes of the compiler. THAT is what's wrong with var.

                                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                -----
                                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                -----
                                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                M 1 Reply Last reply
                                0
                                • R realJSOP

                                  var is a Microsoft mechanism that allows VB last-resorters to be okay with using as more appropriate strongly-typed language rather than comply with the actual paradigm of being a real programmer. In essence, it is the "safe space" of programming constructs and as such, is used only by flaming lib-tards that think all code should be equal in the eyes of the compiler. THAT is what's wrong with var.

                                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                  -----
                                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                  -----
                                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                  M Offline
                                  M Offline
                                  Maarten1977
                                  wrote on last edited by
                                  #19

                                  I think you're flaming 😂😂

                                  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