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. Two of my most frequent issues with .net inconsistencies

Two of my most frequent issues with .net inconsistencies

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
13 Posts 7 Posters 102 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.
  • Greg UtasG Greg Utas

    I'm guessing it has to do with people's obsessions about breaking changes.

    Robust Services Core | Software Techniques for Lemmings | Articles
    The fox knows many things, but the hedgehog knows one big thing.

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

    That's why they can't fix it now, but it doesn't explain how things got that way to begin with, from v1 and earlier.

    Greg UtasG E 2 Replies Last reply
    0
    • P PIEBALDconsult

      That's why they can't fix it now, but it doesn't explain how things got that way to begin with, from v1 and earlier.

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #5

      From v1? Gee. Maybe there were bun fights over the interface. :)

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • P PIEBALDconsult

        In my opinion... .net should never have been released to the public with such inconsistencies as these. :doh:

        System.DateTime.UtcNow .ToString ( "hh:mm:ss" ) ;
        System.DateTime.UtcNow.TimeOfDay.ToString ( "hh\\:mm\\:ss" ) ;

        new System.ArgumentException ( message , paramName ) ;
        new System.ArgumentNullException ( paramName , message ) ;

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

        The DateTime vs TimeSpan is more insidious than you think. DateTime.UtcNow.TimeOfDay.ToString("hh\\:mm\\:ss") will always use ":" as the separator, whereas DateTime.UtcNow.ToString("hh:mm:ss") will use the current culture's time separator. Whilst the default seems to use ":" consistently, I believe the user can override this in the OS settings.

        var format = new DateTimeFormatInfo { TimeSeparator = "#" };
        DateTime.UtcNow.ToString("hh:mm:ss", format); // 07#56#00
        DateTime.UtcNow.TimeOfDay.ToString("hh\\:mm\\:ss", format); // 07:56:00

        The argument exception one is annoying, but I can sort-of understand how it came to be. With an ArgumentNullException, there's an obvious default error message, and the main thing you care about is the name of the argument which was null. But with an ArgumentException, there's no obvious default error message, and the error could be caused by a combination of parameters, so the message is the main thing, and the parameter name is optional. Since C# 4 introduced named parameters, you can use them to make the invocations consistent:

        new ArgumentException(paramName: nameof(foo), message: "Bar");
        new ArgumentNullException(paramName: nameof(foo), message: "Bar");

        NB: Starting with .NET 6, the recommendation is to use ArgumentNullException.ThrowIfNull(parameter); instead of if (parameter is null) throw new ArgumentNullException(nameof(parameter));: ArgumentNullException.ThrowIfNull Method (System) | Microsoft Docs[^] Introduce static methods to allocate and throw key exception types. · Issue #48573 · dotnet/runtime · GitHub[^]


        "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

        P 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          The DateTime vs TimeSpan is more insidious than you think. DateTime.UtcNow.TimeOfDay.ToString("hh\\:mm\\:ss") will always use ":" as the separator, whereas DateTime.UtcNow.ToString("hh:mm:ss") will use the current culture's time separator. Whilst the default seems to use ":" consistently, I believe the user can override this in the OS settings.

          var format = new DateTimeFormatInfo { TimeSeparator = "#" };
          DateTime.UtcNow.ToString("hh:mm:ss", format); // 07#56#00
          DateTime.UtcNow.TimeOfDay.ToString("hh\\:mm\\:ss", format); // 07:56:00

          The argument exception one is annoying, but I can sort-of understand how it came to be. With an ArgumentNullException, there's an obvious default error message, and the main thing you care about is the name of the argument which was null. But with an ArgumentException, there's no obvious default error message, and the error could be caused by a combination of parameters, so the message is the main thing, and the parameter name is optional. Since C# 4 introduced named parameters, you can use them to make the invocations consistent:

          new ArgumentException(paramName: nameof(foo), message: "Bar");
          new ArgumentNullException(paramName: nameof(foo), message: "Bar");

          NB: Starting with .NET 6, the recommendation is to use ArgumentNullException.ThrowIfNull(parameter); instead of if (parameter is null) throw new ArgumentNullException(nameof(parameter));: ArgumentNullException.ThrowIfNull Method (System) | Microsoft Docs[^] Introduce static methods to allocate and throw key exception types. · Issue #48573 · dotnet/runtime · GitHub[^]


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

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

          I don't use .net 6 . How would I specify the message though? Edit: In fact I don't use any features of C# 4 either. And it may be that the only features I use of C# 3 are Extension Method, Collection Initializer, and Object Initializer. I just ran through some of my libraries, seeing which require C# 3. Most do, but one will actually compile with C# 2. And I had actually forgotten about Collection Initializer until this week -- I used it in two places in a piece of code I wrote in 2014, to initialize a Dictionary. Last week I wrote some code which used a Dictionary Initializer, not realizing it is a C# 6 feature, and then rewrote it as a Collection Initializer when I found out. In the code I am working on, the Dictionary Initializer provides no benefit over the Collection Initializer.

          Richard DeemingR 1 Reply Last reply
          0
          • P PIEBALDconsult

            I don't use .net 6 . How would I specify the message though? Edit: In fact I don't use any features of C# 4 either. And it may be that the only features I use of C# 3 are Extension Method, Collection Initializer, and Object Initializer. I just ran through some of my libraries, seeing which require C# 3. Most do, but one will actually compile with C# 2. And I had actually forgotten about Collection Initializer until this week -- I used it in two places in a piece of code I wrote in 2014, to initialize a Dictionary. Last week I wrote some code which used a Dictionary Initializer, not realizing it is a C# 6 feature, and then rewrote it as a Collection Initializer when I found out. In the code I am working on, the Dictionary Initializer provides no benefit over the Collection Initializer.

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

            You wouldn't; the ThrowIfNull method always uses the default error message, which leads to more consistent errors. You could override the parameter name, but it's best to let the compiler fill it in using the [CallerArgumentExpression] attribute.


            "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

            P 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              You wouldn't; the ThrowIfNull method always uses the default error message, which leads to more consistent errors. You could override the parameter name, but it's best to let the compiler fill it in using the [CallerArgumentExpression] attribute.


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

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

              Seems pointless anyway. Why would I want a "default error message"? Or, if I do, I'd make that an inner exception and provide a meaningful message in the main exception. So far I have found no useful features of C# 6... except... I just found that "dictionary initializer" is a C# 6 feature and I have been toying with that for a week or so now. Why didn't this exist since the advent of initializers? I also just dabbled with string interpolation -- which I heard about, but hadn't bothered with -- and, as expected, it provides no benefit to me. It works only with string literals, and most format strings I use are not literals. It looks like it would lead to bad practices. In particular, how do you use string interpolation with globalization? Anyway, now that I know that I am using a v6 compiler, I can see if that's why my recent builds refuse to execute on the servers. I'll specify v4 and see if they succeed then. Edit: In case anyone is curious. Yes, once I rebuilt with an earlier compiler (v5), I was able to run the utility on the server. I am not in control of what versions of things (e.g. .net) are installed on my laptop and the servers, but it's never the latest version.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                In my opinion... .net should never have been released to the public with such inconsistencies as these. :doh:

                System.DateTime.UtcNow .ToString ( "hh:mm:ss" ) ;
                System.DateTime.UtcNow.TimeOfDay.ToString ( "hh\\:mm\\:ss" ) ;

                new System.ArgumentException ( message , paramName ) ;
                new System.ArgumentNullException ( paramName , message ) ;

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #10

                Aren't there similar quirks with the DateFormat? And what about NumberFormat (like decimal separator etc.)?

                Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                P 1 Reply Last reply
                0
                • B Bernhard Hiller

                  Aren't there similar quirks with the DateFormat? And what about NumberFormat (like decimal separator etc.)?

                  Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

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

                  I think so. Plus in the past I've run into issues with other limitations on what can be specified in format strings. Which led to this : ApplyFormat[^] And this week I've been working on another formatter, kinda sorta similar to string interpolation, but not really.

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    In my opinion... .net should never have been released to the public with such inconsistencies as these. :doh:

                    System.DateTime.UtcNow .ToString ( "hh:mm:ss" ) ;
                    System.DateTime.UtcNow.TimeOfDay.ToString ( "hh\\:mm\\:ss" ) ;

                    new System.ArgumentException ( message , paramName ) ;
                    new System.ArgumentNullException ( paramName , message ) ;

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

                    How much money should they spend, in your opinion, to have parameters in the same order, even if it doesn't make sense? :|

                    Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      That's why they can't fix it now, but it doesn't explain how things got that way to begin with, from v1 and earlier.

                      E Offline
                      E Offline
                      englebart
                      wrote on last edited by
                      #13

                      They probably copied it from Java and had to swap the order to avoid copyright infringement. Before the judge: See Java has its parameters in the same order for both. DotNet 1.0 is different.

                      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