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. Other Discussions
  3. The Weird and The Wonderful
  4. C#'s sneaky typedef

C#'s sneaky typedef

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
37 Posts 16 Posters 4 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.
  • S Simon ORiordan from UK

    I haven't used var except where essential in almost 10 years. :-D

    B Offline
    B Offline
    BobJanova
    wrote on last edited by
    #16

    I don't like it except in type declaration plus initialise statements ... there's no point doubling up the type information in

    var dict = new Dictionary<String, IList<DataColumn>>();

    But of course one of the places you can't use it is in field declarations which is where you want to do that a lot! It's also a bit ugly writing code that saves a Linq query if you declare the type (IQueryable<T>, right?). It seems to be standard to use var there, although I've been known to put the actual type instead.

    L K 2 Replies Last reply
    0
    • B BobJanova

      I don't like it except in type declaration plus initialise statements ... there's no point doubling up the type information in

      var dict = new Dictionary<String, IList<DataColumn>>();

      But of course one of the places you can't use it is in field declarations which is where you want to do that a lot! It's also a bit ugly writing code that saves a Linq query if you declare the type (IQueryable<T>, right?). It seems to be standard to use var there, although I've been known to put the actual type instead.

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #17

      BobJanova wrote:

      var dict = new Dictionary<String, IList<DataColumn>>();

      var columnNameDict = new Dictionary>();

      FTFY Just to ensure that nobody use your dict to find a way to a cathouse. Or something like that. BTW. I'd like to have this syntax:

      Dictionary<String, IList<DataColumn>> dict = new();

      I'd have information on type in a more logical place and could concentrate on parameters passed to a constructor. And still no doubling.

      Greetings - Jacek

      B L 2 Replies Last reply
      0
      • L Lutoslaw

        BobJanova wrote:

        var dict = new Dictionary<String, IList<DataColumn>>();

        var columnNameDict = new Dictionary>();

        FTFY Just to ensure that nobody use your dict to find a way to a cathouse. Or something like that. BTW. I'd like to have this syntax:

        Dictionary<String, IList<DataColumn>> dict = new();

        I'd have information on type in a more logical place and could concentrate on parameters passed to a constructor. And still no doubling.

        Greetings - Jacek

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #18

        Yes fair point with the name there. I'd like a type syntax like that (well maybe not exactly like that, it looks a bit weird, but similar) as well, but since we don't and we do have var, it deputises quite well.

        1 Reply Last reply
        0
        • L Lutoslaw

          BobJanova wrote:

          var dict = new Dictionary<String, IList<DataColumn>>();

          var columnNameDict = new Dictionary>();

          FTFY Just to ensure that nobody use your dict to find a way to a cathouse. Or something like that. BTW. I'd like to have this syntax:

          Dictionary<String, IList<DataColumn>> dict = new();

          I'd have information on type in a more logical place and could concentrate on parameters passed to a constructor. And still no doubling.

          Greetings - Jacek

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

          Looks nice, but violates the (already violated) rule that the type of an expression is determined by its parts, not by the context in which it appears. Of course that rule is already broken by integer constants.. and null cheats with its "null type" that is implicitly convertible to many types. So I don't know.

          B 1 Reply Last reply
          0
          • L Lost User

            Looks nice, but violates the (already violated) rule that the type of an expression is determined by its parts, not by the context in which it appears. Of course that rule is already broken by integer constants.. and null cheats with its "null type" that is implicitly convertible to many types. So I don't know.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #20

            I don't think that's really a rule any more. What's the type of the lambda x => x + 1? You can't tell without looking at the calling context.

            L 1 Reply Last reply
            0
            • B BobJanova

              I don't think that's really a rule any more. What's the type of the lambda x => x + 1? You can't tell without looking at the calling context.

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

              It's still a rule, it just doesn't apply everywhere. The situation for lambda's is particularly bad[^], but that's no excuse to infect the rest of language with such nonsense.

              1 Reply Last reply
              0
              • S Simon ORiordan from UK

                I haven't used var except where essential in almost 10 years. :-D

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

                Why 10 years? Var was introduced with C# 3.0, which was released about 6 years ago. I hope you weren't trying to use it before then. :doh:

                Thou mewling ill-breeding pignut!

                S 1 Reply Last reply
                0
                • L Lost User

                  I was completely flabbergasted by a piece of C# code, until I saw one line near the top (hidden at first in a collapsed block) that read

                  using var = System.Int32;

                  Wow, OK. Yes, you can do that, and yes, that makes var (note the colour) behave exactly like int (well like Int32 really - that is, you can't use it as the base type of an enum), and yes, this forum is highlighting it with the wrong colour in the code block.

                  C Offline
                  C Offline
                  Chad3F
                  wrote on last edited by
                  #23

                  Seems about the same as doing this in C or C++: /* What could possibly go wrong */ #define int double Since the substitutions are done before the tokens are interpreted.

                  1 Reply Last reply
                  0
                  • S Simon ORiordan from UK

                    I haven't used var except where essential in almost 10 years. :-D

                    K Offline
                    K Offline
                    KP Lee
                    wrote on last edited by
                    #24

                    Simon O'Riordan from UK wrote:

                    I haven't used var except where essential in almost 10 years.

                    Wow, you must be really advanced. When I took C# training in 2005 var never came up and I read the manuals from cover to cover and never saw the command. I was under the impression it was introduced in 2005. (I can easily be totally wrong about that.) When is it ever essential? There is always:

                    object x = ...

                    S J 2 Replies Last reply
                    0
                    • B BobJanova

                      I don't like it except in type declaration plus initialise statements ... there's no point doubling up the type information in

                      var dict = new Dictionary<String, IList<DataColumn>>();

                      But of course one of the places you can't use it is in field declarations which is where you want to do that a lot! It's also a bit ugly writing code that saves a Linq query if you declare the type (IQueryable<T>, right?). It seems to be standard to use var there, although I've been known to put the actual type instead.

                      K Offline
                      K Offline
                      KP Lee
                      wrote on last edited by
                      #25

                      BobJanova wrote:

                      But of course one of the places you can't use it is in field declarations which is where you want to do that a lot!

                      Of course you can! Just do what the original poster had found being done to it. :laugh:

                      1 Reply Last reply
                      0
                      • A AspDotNetDev

                        Why 10 years? Var was introduced with C# 3.0, which was released about 6 years ago. I hope you weren't trying to use it before then. :doh:

                        Thou mewling ill-breeding pignut!

                        S Offline
                        S Offline
                        Simon ORiordan from UK
                        wrote on last edited by
                        #26

                        Long ago, in a galaxy far far away, was something called Visual Basic.

                        A 1 Reply Last reply
                        0
                        • K KP Lee

                          Simon O'Riordan from UK wrote:

                          I haven't used var except where essential in almost 10 years.

                          Wow, you must be really advanced. When I took C# training in 2005 var never came up and I read the manuals from cover to cover and never saw the command. I was under the impression it was introduced in 2005. (I can easily be totally wrong about that.) When is it ever essential? There is always:

                          object x = ...

                          S Offline
                          S Offline
                          Simon ORiordan from UK
                          wrote on last edited by
                          #27

                          It was present in Visual Basic. Before .Net was a squirt in a squirts imagination.

                          R 1 Reply Last reply
                          0
                          • S Simon ORiordan from UK

                            Long ago, in a galaxy far far away, was something called Visual Basic.

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

                            You are thinking of the VB.NET variant type, which is not the same thing as C#'s implicitly typed "var". In C#, a var variable will have a compile-time type defined by the type on the right hand side of the assignment. In VB.NET, a variant type can change at runtime (unlike in C#). However, VB.NET now has the ability to implicitly type variables, just as with C#'s var. However, I think in VB.NET, you just do that by leaving off the type (e.g., Dim x = 5). Variants are an abomination. Implicitly typed variables are necessary (e.g., for anonymous types), and can be nice (e.g., for very long type declarations). You can read more about implicitly typed variables here. The closest thing to VB.NET's variant type in C# would be a variable of type "Object".

                            Thou mewling ill-breeding pignut!

                            S 1 Reply Last reply
                            0
                            • A AspDotNetDev

                              You are thinking of the VB.NET variant type, which is not the same thing as C#'s implicitly typed "var". In C#, a var variable will have a compile-time type defined by the type on the right hand side of the assignment. In VB.NET, a variant type can change at runtime (unlike in C#). However, VB.NET now has the ability to implicitly type variables, just as with C#'s var. However, I think in VB.NET, you just do that by leaving off the type (e.g., Dim x = 5). Variants are an abomination. Implicitly typed variables are necessary (e.g., for anonymous types), and can be nice (e.g., for very long type declarations). You can read more about implicitly typed variables here. The closest thing to VB.NET's variant type in C# would be a variable of type "Object".

                              Thou mewling ill-breeding pignut!

                              S Offline
                              S Offline
                              Simon ORiordan from UK
                              wrote on last edited by
                              #29

                              Ah, I see. Thanks for that. Agree, variants are awful; implicit typing? Doesn't sound too helpful for complex walkthrough's, although I love it in Python.

                              A 1 Reply Last reply
                              0
                              • S Simon ORiordan from UK

                                Ah, I see. Thanks for that. Agree, variants are awful; implicit typing? Doesn't sound too helpful for complex walkthrough's, although I love it in Python.

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

                                Not sure what you mean by "complex walkthroughs".

                                Thou mewling ill-breeding pignut!

                                S 1 Reply Last reply
                                0
                                • A AspDotNetDev

                                  Not sure what you mean by "complex walkthroughs".

                                  Thou mewling ill-breeding pignut!

                                  S Offline
                                  S Offline
                                  Simon ORiordan from UK
                                  wrote on last edited by
                                  #31

                                  One job I had meant examining code by eye for the most part. Would have meant a lot of extra difficulty if the variables had not been explicitly declared. It was a mixture of VC6 and C# interop. The VC6 couldn't be unit tested, it was a 500,000 LOC chunk that could be run through the VS6 debugger, but not split up. Horrible code.

                                  1 Reply Last reply
                                  0
                                  • S Simon ORiordan from UK

                                    It was present in Visual Basic. Before .Net was a squirt in a squirts imagination.

                                    R Offline
                                    R Offline
                                    Rob Grainger
                                    wrote on last edited by
                                    #32

                                    Er, no it wasn't. VB 6 had variant's but that's a different and altogether more abhorrent kettle of fish[^].

                                    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                    S 1 Reply Last reply
                                    0
                                    • R Rob Grainger

                                      Er, no it wasn't. VB 6 had variant's but that's a different and altogether more abhorrent kettle of fish[^].

                                      "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                      S Offline
                                      S Offline
                                      Simon ORiordan from UK
                                      wrote on last edited by
                                      #33

                                      Yes. We sorted all that out last week. Do try and keep up 007.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        I was completely flabbergasted by a piece of C# code, until I saw one line near the top (hidden at first in a collapsed block) that read

                                        using var = System.Int32;

                                        Wow, OK. Yes, you can do that, and yes, that makes var (note the colour) behave exactly like int (well like Int32 really - that is, you can't use it as the base type of an enum), and yes, this forum is highlighting it with the wrong colour in the code block.

                                        G Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #34

                                        What's old is new. I found the following:

                                        #define void int

                                        in some C code when I started my current job.

                                        Software Zen: delete this;

                                        1 Reply Last reply
                                        0
                                        • K KP Lee

                                          Simon O'Riordan from UK wrote:

                                          I haven't used var except where essential in almost 10 years.

                                          Wow, you must be really advanced. When I took C# training in 2005 var never came up and I read the manuals from cover to cover and never saw the command. I was under the impression it was introduced in 2005. (I can easily be totally wrong about that.) When is it ever essential? There is always:

                                          object x = ...

                                          J Offline
                                          J Offline
                                          johannesnestler
                                          wrote on last edited by
                                          #35

                                          Sorry for adding a late comment - couldn't let your "object x =" stand there. This is NOT the same as using var (because in C# var means "I don't want to type - compiler look it up", or "I have an anonymouse type the compiler will know it's name later - but I don't!". Where holding a value as it's base type (in C# object is the base type of all reference types) is a complete other story!

                                          K 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