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.
  • 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.

    S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #5

    Awesome! Can you some other creating stuff? like. I dunno...

    using int = System.String;

    ?! :P

    My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

    L 1 Reply Last reply
    0
    • S Super Lloyd

      Awesome! Can you some other creating stuff? like. I dunno...

      using int = System.String;

      ?! :P

      My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

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

      No you can't just throw any keyword in there, it has to be a contextual keyword that isn't a keyword in that context. For example:

      using from = System.SByte;
      using let = System.Byte;
      using orderby = System.Single;
      using select = System.String;

      T 1 Reply Last reply
      0
      • L Lost User

        No you can't just throw any keyword in there, it has to be a contextual keyword that isn't a keyword in that context. For example:

        using from = System.SByte;
        using let = System.Byte;
        using orderby = System.Single;
        using select = System.String;

        T Offline
        T Offline
        Thomas Daniels
        wrote on last edited by
        #7

        And this also works:

        using System;
        ...
        using String = System.Int32;

        And:

        String s = "str"; // throws an error
        String s1 = 5; // this doesn't throw an error

        The quick red ProgramFOX jumps right over the Lazy<Dog>. My latest article: Understand how bitwise operators work (C# and VB.NET examples) My group: C# Programmers Group

        L 1 Reply Last reply
        0
        • T Thomas Daniels

          And this also works:

          using System;
          ...
          using String = System.Int32;

          And:

          String s = "str"; // throws an error
          String s1 = 5; // this doesn't throw an error

          The quick red ProgramFOX jumps right over the Lazy<Dog>. My latest article: Understand how bitwise operators work (C# and VB.NET examples) My group: C# Programmers Group

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

          String is not a keyword, so that's not very surprising..

          K 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Find them. Then kill them. Horribly. A lesson must be sent out: do not do this.

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            O Offline
            O Offline
            Oshtri Deka
            wrote on last edited by
            #9

            A bit dramatic, but generally I agree.

            Mislim, dakle jeo sam.

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Find them. Then kill them. Horribly. A lesson must be sent out: do not do this.

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

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

              Precisely my initial reaction. Their head should be mounted on a pike outside the cube farm's walls as a warning to others.

              Software Zen: delete this;

              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.

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

                I kind of had your first responder's reaction to this code, then recalled my irritation with code that used var and forced me to look-up the return type of the function to figure out what the object was. So, my second reaction was YA, someone is forcing the lazy programmer to stop using the lazy var keyword. I want to kill 'm and sing his/her praises. You could say I'm conflicted.

                B 1 Reply Last reply
                0
                • L Lost User

                  String is not a keyword, so that's not very surprising..

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

                  harold aptroot wrote:

                  String is not a keyword, so that's not very surprising..

                  Agreed. My first reaction too.

                  1 Reply Last reply
                  0
                  • K KP Lee

                    I kind of had your first responder's reaction to this code, then recalled my irritation with code that used var and forced me to look-up the return type of the function to figure out what the object was. So, my second reaction was YA, someone is forcing the lazy programmer to stop using the lazy var keyword. I want to kill 'm and sing his/her praises. You could say I'm conflicted.

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

                    If it was the second then they should have linked it to a class called DoNotUseVar or something.

                    K 1 Reply Last reply
                    0
                    • B BobJanova

                      If it was the second then they should have linked it to a class called DoNotUseVar or something.

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

                      BobJanova wrote:

                      they should have linked it to a class called DoNotUseVar or something.

                      :laugh: The casting error would certainly pop out better. Less confusing than the unsuspected error generated by:

                      byte a = 10;
                      var b = a;
                      byte c = b;

                      S 1 Reply Last reply
                      0
                      • K KP Lee

                        BobJanova wrote:

                        they should have linked it to a class called DoNotUseVar or something.

                        :laugh: The casting error would certainly pop out better. Less confusing than the unsuspected error generated by:

                        byte a = 10;
                        var b = a;
                        byte c = b;

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

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

                        B A K 3 Replies Last reply
                        0
                        • 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
                                          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