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. .Net Core 6 jumps the shark

.Net Core 6 jumps the shark

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpdotnetjavascriptasp-netwpf
41 Posts 14 Posters 235 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.
  • raddevusR raddevus

    File this one under Weird. I just built a new project in .NET Core 6 using the command:

    c:\> dotnet new console -o SuperConsole

    This produced a basic console app that outputs "Hello, World!" Then I took a look at Program.cs and noticed that the entirety of the code is:

    // See https://aka.ms/new-console-template for more information
    Console.WriteLine("Hello, World!");

    No Main Function!?! Yep, that's right. There is no main function!! Here's the link that explains it. C# template changes in .NET 6 - .NET | Microsoft Docs[^] Here's a bit of explanation from the site:

    Explanation at link:

    You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can even add functions. They're created as local functions nested inside the generated Main method.

    I'ma no on that. Functions nested in Main? :| And it tells you that you can still add in the Main method the old way yourself and use it that way. Is C# trying to be JavaScript?

    M Offline
    M Offline
    Member 9167057
    wrote on last edited by
    #15

    That's a simple continuation of the "pay for play" philosophy. While the unavailability of the Main function is a thing I really hate about Python (how the hell am I supposed to know where complex code starts operating), it's absence is a huge win for small code bases. Don't get me wrong, for a kLoC of code, spread across 4 or so different modules, the lack of structure which this particular C# template brings to the table would be a bloody nightmare (which is why I'm not using this style for my kLoC-multimodule project). But for something of only mild complexity, that's a win. Boilerplate code, like any other overhead, starts paying off eventually, but if you have something not nearly huge enough for that overhead to pay off, low-overhead alternatives rule. Take file system as an example. NTFS (or ext, if you're so inclined) is by orders of magnitude more advanced, than FAT. Yet, FAT (be it FAT32 or exFAT) got it's own raison d'etre, which is low-requirements-low-overhead. PS: that part that you highlighted, namely local functions, is older, than .NET 6. They started with C# 7.0 which started it's life with .NET 4*x.

    1 Reply Last reply
    0
    • raddevusR raddevus

      File this one under Weird. I just built a new project in .NET Core 6 using the command:

      c:\> dotnet new console -o SuperConsole

      This produced a basic console app that outputs "Hello, World!" Then I took a look at Program.cs and noticed that the entirety of the code is:

      // See https://aka.ms/new-console-template for more information
      Console.WriteLine("Hello, World!");

      No Main Function!?! Yep, that's right. There is no main function!! Here's the link that explains it. C# template changes in .NET 6 - .NET | Microsoft Docs[^] Here's a bit of explanation from the site:

      Explanation at link:

      You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can even add functions. They're created as local functions nested inside the generated Main method.

      I'ma no on that. Functions nested in Main? :| And it tells you that you can still add in the Main method the old way yourself and use it that way. Is C# trying to be JavaScript?

      M Offline
      M Offline
      MSBassSinger
      wrote on last edited by
      #16

      There are a number of shorthand changes (but little innovation) that have been made to C# over the years that are of limited or questionable value. It is a good idea to test out these shorthand C# changes, then look at what the compiler does with them by looking at the generated MSIL. As one example, having done that, it is why I no longer use "using" for IDisposable objects. If you like a particular shortcut, use it. But my advice is to at least know what the compiler does with it. In the case of the OP, just make your own Main() and go with it.

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Kind of. The C# script would be compiled and executed without generating an .EXE on disk. It would all be in-memory. At least, that's the plan. The "script" support for previous C# versions sucks as I recall. It's been a few years since I've dabbled with it.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        M Offline
        M Offline
        MSBassSinger
        wrote on last edited by
        #17

        I did that a few years ago. It makes a nice scripting utility. I had to figure out what shortcuts in the IDE were not in the compiler, but that did not take long. Mine worked out well, and I used it in a production app. I added in some safeguards, like making sure the C# script had not been tampered with (lots of opportunity for a disgruntled employee to alter an existing script to wreck havoc on production systems).

        1 Reply Last reply
        0
        • raddevusR raddevus

          File this one under Weird. I just built a new project in .NET Core 6 using the command:

          c:\> dotnet new console -o SuperConsole

          This produced a basic console app that outputs "Hello, World!" Then I took a look at Program.cs and noticed that the entirety of the code is:

          // See https://aka.ms/new-console-template for more information
          Console.WriteLine("Hello, World!");

          No Main Function!?! Yep, that's right. There is no main function!! Here's the link that explains it. C# template changes in .NET 6 - .NET | Microsoft Docs[^] Here's a bit of explanation from the site:

          Explanation at link:

          You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can even add functions. They're created as local functions nested inside the generated Main method.

          I'ma no on that. Functions nested in Main? :| And it tells you that you can still add in the Main method the old way yourself and use it that way. Is C# trying to be JavaScript?

          Z Offline
          Z Offline
          zezba9000
          wrote on last edited by
          #18

          Even VS 2022 does this. I hate it & its not the correct way to be writing anything in C#.

          1 Reply Last reply
          0
          • M MSBassSinger

            There are a number of shorthand changes (but little innovation) that have been made to C# over the years that are of limited or questionable value. It is a good idea to test out these shorthand C# changes, then look at what the compiler does with them by looking at the generated MSIL. As one example, having done that, it is why I no longer use "using" for IDisposable objects. If you like a particular shortcut, use it. But my advice is to at least know what the compiler does with it. In the case of the OP, just make your own Main() and go with it.

            J Offline
            J Offline
            Jon McKee
            wrote on last edited by
            #19

            Could you expand on what you meant by the using example? From what I can see, these end up equivalent:

            using (SomeResource res = new SomeResource())
            {
            //using block
            }
            //-------------
            SomeResource res = new SomeResource();
            try
            {
            //using block
            }
            finally
            {
            if (res != null)
            ((IDisposable)res).Dispose();
            }

            which seems right to me.

            M 1 Reply Last reply
            0
            • J Jon McKee

              Could you expand on what you meant by the using example? From what I can see, these end up equivalent:

              using (SomeResource res = new SomeResource())
              {
              //using block
              }
              //-------------
              SomeResource res = new SomeResource();
              try
              {
              //using block
              }
              finally
              {
              if (res != null)
              ((IDisposable)res).Dispose();
              }

              which seems right to me.

              M Offline
              M Offline
              MSBassSinger
              wrote on last edited by
              #20

              In short, the using statement swallows constructor errors. Since the actual code being executed is a try … finally, why not just use try … finally (or better yet, try… catch … finally) and use your own code for capturing and logging all exception? And given the unpredictability of the GC, scalability is better served by following the principle, “if you create an object, clean it up when done with it”. Relying on the GC and using shortcuts like the using statement are things I consider poor engineering choices in the context of the SDLC. Others may disagree, but I have yet to see a reasoned argument against my approach that ends in better software.

              J 1 Reply Last reply
              0
              • M MSBassSinger

                In short, the using statement swallows constructor errors. Since the actual code being executed is a try … finally, why not just use try … finally (or better yet, try… catch … finally) and use your own code for capturing and logging all exception? And given the unpredictability of the GC, scalability is better served by following the principle, “if you create an object, clean it up when done with it”. Relying on the GC and using shortcuts like the using statement are things I consider poor engineering choices in the context of the SDLC. Others may disagree, but I have yet to see a reasoned argument against my approach that ends in better software.

                J Offline
                J Offline
                Jon McKee
                wrote on last edited by
                #21

                Aah, ok. I can understand not wanting to use the pattern if you need to catch exceptions or heavily expect that you will need to in the future with that class (if you're gonna have to expand the using into a try...catch anyways might as well go ahead and do it). That being said, if the exceptions happen in the constructor, they will not be swallowed since object creation happens outside of the try...finally. The only exceptions that would be swallowed are the ones that occur while using the object. Tested using:

                class Test : IDisposable
                {
                public Test() => throw new NotImplementedException();

                public void Dispose() {}
                

                }

                using (Test t = new Test())
                {
                Console.WriteLine("constructor exception swallowed.");
                }

                M 1 Reply Last reply
                0
                • J Jon McKee

                  Aah, ok. I can understand not wanting to use the pattern if you need to catch exceptions or heavily expect that you will need to in the future with that class (if you're gonna have to expand the using into a try...catch anyways might as well go ahead and do it). That being said, if the exceptions happen in the constructor, they will not be swallowed since object creation happens outside of the try...finally. The only exceptions that would be swallowed are the ones that occur while using the object. Tested using:

                  class Test : IDisposable
                  {
                  public Test() => throw new NotImplementedException();

                  public void Dispose() {}
                  

                  }

                  using (Test t = new Test())
                  {
                  Console.WriteLine("constructor exception swallowed.");
                  }

                  M Offline
                  M Offline
                  MSBassSinger
                  wrote on last edited by
                  #22

                  I did a demo on this about 1-1/2 years ago, as my fellow developers didn’t see the harm, either. They did afterwards. Since in a well designed app, the try-catch-finally is mostly copy and paste, it really does not save any meaningful development time to use the using statement. Exception handling is a key to reducing dev and QA testing, as well as production troubleshooting. By utilizing the exception’s Data collection, the developer can capture runtime values that are very helpful in diagnosing problems in execution. I have, on many occasions, seen production troubleshooting that would have taken a day or more, shortened to minutes, by smart exception handling. In many production systems, that difference in time can mean thousands to millions in revenue losses avoided by significantly quicker resolution. Using us a shortcut that alleviates the burden of a developer having to remember to call Dispose(). I’d rather use developers who don’t need such shortcuts.

                  J Richard DeemingR 2 Replies Last reply
                  0
                  • M MSBassSinger

                    I did a demo on this about 1-1/2 years ago, as my fellow developers didn’t see the harm, either. They did afterwards. Since in a well designed app, the try-catch-finally is mostly copy and paste, it really does not save any meaningful development time to use the using statement. Exception handling is a key to reducing dev and QA testing, as well as production troubleshooting. By utilizing the exception’s Data collection, the developer can capture runtime values that are very helpful in diagnosing problems in execution. I have, on many occasions, seen production troubleshooting that would have taken a day or more, shortened to minutes, by smart exception handling. In many production systems, that difference in time can mean thousands to millions in revenue losses avoided by significantly quicker resolution. Using us a shortcut that alleviates the burden of a developer having to remember to call Dispose(). I’d rather use developers who don’t need such shortcuts.

                    J Offline
                    J Offline
                    Jon McKee
                    wrote on last edited by
                    #23

                    I view it as more of an option to reduce code noise in specific circumstances. I don't disagree with your examples but I don't see the path to the conclusion of inherent harm. Playing devil's advocate on myself, I can see the argument that maybe the class itself won't throw once constructed but something could go terribly wrong with the resource. On the one hand if that's somewhat expected like with a network connection, I can see the merit in saying using would be harmful for the reasons you've stated. On the other hand, if errors in the resource are unrecoverable at this layer, then I would argue using explicitly states your expectation - this code should not throw and if it does something has gone horribly wrong we can't recover from here so let the exception propogate upwards. In this situation the whole point is the finally block, not the exception handling. I agree it can be misused but non-recovering catches are often misused by improperly re-throwing and losing stack-trace information. I still see the value in re-throwing though if you want to add explicit information to the exception's data to help with debugging.

                    1 Reply Last reply
                    0
                    • M MSBassSinger

                      I did a demo on this about 1-1/2 years ago, as my fellow developers didn’t see the harm, either. They did afterwards. Since in a well designed app, the try-catch-finally is mostly copy and paste, it really does not save any meaningful development time to use the using statement. Exception handling is a key to reducing dev and QA testing, as well as production troubleshooting. By utilizing the exception’s Data collection, the developer can capture runtime values that are very helpful in diagnosing problems in execution. I have, on many occasions, seen production troubleshooting that would have taken a day or more, shortened to minutes, by smart exception handling. In many production systems, that difference in time can mean thousands to millions in revenue losses avoided by significantly quicker resolution. Using us a shortcut that alleviates the burden of a developer having to remember to call Dispose(). I’d rather use developers who don’t need such shortcuts.

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

                      A using block does not swallow exceptions, any more than a try..finally would. The only time an exception would be lost would be if your Dispose method throws an exception. Whilst that's not entirely unheard of, it's a sign of a poorly implemented class, not a reason to reject the using block outright - especially since you'd have exactly the same problem with a try..finally block. Code written with a using block is going to be significantly more "correct" than code that eschews it in favour of manual clean-up. Maybe you should post your demo code to try to convince us.


                      "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

                      M 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        A using block does not swallow exceptions, any more than a try..finally would. The only time an exception would be lost would be if your Dispose method throws an exception. Whilst that's not entirely unheard of, it's a sign of a poorly implemented class, not a reason to reject the using block outright - especially since you'd have exactly the same problem with a try..finally block. Code written with a using block is going to be significantly more "correct" than code that eschews it in favour of manual clean-up. Maybe you should post your demo code to try to convince us.


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

                        M Offline
                        M Offline
                        MSBassSinger
                        wrote on last edited by
                        #25

                        Quote:

                        Code written with a using block is going to be significantly more "correct" than code that eschews it in favour of manual clean-up.

                        That comes across as more religion than a rational approach. In order to illustrate my point, I can add the C# code I used for the test (in .NET 6.0) and the MSIL output of both. The unit test for the "using" statement took 28 ms, while the standard use took 18 ms. The MSIL for the "using" statement produced 47 lines of MSIL code, while the standard approach without "using" produced 31 lines of MSIL code. I kept the IDisposable instance simple for this example. In order to capture the constructor exception, the using block has to be wrapped with a try-catch. The results of this test, combined with how I capture exception data and the runtime values associated with the exception (to significantly reduce support costs of the SDLC), is why I do not use the "using" statement in production apps. I do use it where appropriate in proof-of-concept and personal utilities, where support and performance is less of a concern. IDisposable class:

                        namespace UsingTest
                        {
                        public class DisposableClass : IDisposable
                        {

                        	private Boolean m\_blnDisposeHasBeenCalled = false;
                        
                        	public DisposableClass()
                        	{
                        		Int32 denom = 20;
                        		Int32 numer = 0;
                        		Int32 result = denom / numer;
                        
                        	}
                        
                        	public String WhoIsIt()
                        	{
                        		return $"{Environment.UserDomainName}\\\\{Environment.UserName} on {Environment.MachineName} from {Environment.CurrentDirectory} on thread ID {Environment.CurrentManagedThreadId.ToString()}.";
                        	}
                        
                        	#region IDisposable Implementation
                        
                        	/// /// Implement the IDisposable.Dispose() method
                        	/// Developers are supposed to call this method when done with this Object.
                        	/// There is no guarantee when or if the GC will call it, so 
                        	/// the developer is responsible to.  GC does NOT clean up unmanaged 
                        	/// resources, such as COM objects, so we have to clean those up, too.
                        	/// 
                        	/// 
                        	public void Dispose()
                        	{
                        		try
                        		{
                        			// Check if Dispose has already been called 
                        			// Only allow the consumer to call it once with effect.
                        			if (!m\_blnDisposeHasBeenCalled)
                        			{
                        				// Call the overridden Dispose method that contains common cleanup code
                        				// Pass true to indicate that it is called from Dispose
                        				Dispose(true);
                        
                        				// Prevent subsequent finalization of this Object. This is not needed 
                        				// because managed and unmanaged res
                        
                        Richard DeemingR 1 Reply Last reply
                        0
                        • M MSBassSinger

                          Quote:

                          Code written with a using block is going to be significantly more "correct" than code that eschews it in favour of manual clean-up.

                          That comes across as more religion than a rational approach. In order to illustrate my point, I can add the C# code I used for the test (in .NET 6.0) and the MSIL output of both. The unit test for the "using" statement took 28 ms, while the standard use took 18 ms. The MSIL for the "using" statement produced 47 lines of MSIL code, while the standard approach without "using" produced 31 lines of MSIL code. I kept the IDisposable instance simple for this example. In order to capture the constructor exception, the using block has to be wrapped with a try-catch. The results of this test, combined with how I capture exception data and the runtime values associated with the exception (to significantly reduce support costs of the SDLC), is why I do not use the "using" statement in production apps. I do use it where appropriate in proof-of-concept and personal utilities, where support and performance is less of a concern. IDisposable class:

                          namespace UsingTest
                          {
                          public class DisposableClass : IDisposable
                          {

                          	private Boolean m\_blnDisposeHasBeenCalled = false;
                          
                          	public DisposableClass()
                          	{
                          		Int32 denom = 20;
                          		Int32 numer = 0;
                          		Int32 result = denom / numer;
                          
                          	}
                          
                          	public String WhoIsIt()
                          	{
                          		return $"{Environment.UserDomainName}\\\\{Environment.UserName} on {Environment.MachineName} from {Environment.CurrentDirectory} on thread ID {Environment.CurrentManagedThreadId.ToString()}.";
                          	}
                          
                          	#region IDisposable Implementation
                          
                          	/// /// Implement the IDisposable.Dispose() method
                          	/// Developers are supposed to call this method when done with this Object.
                          	/// There is no guarantee when or if the GC will call it, so 
                          	/// the developer is responsible to.  GC does NOT clean up unmanaged 
                          	/// resources, such as COM objects, so we have to clean those up, too.
                          	/// 
                          	/// 
                          	public void Dispose()
                          	{
                          		try
                          		{
                          			// Check if Dispose has already been called 
                          			// Only allow the consumer to call it once with effect.
                          			if (!m\_blnDisposeHasBeenCalled)
                          			{
                          				// Call the overridden Dispose method that contains common cleanup code
                          				// Pass true to indicate that it is called from Dispose
                          				Dispose(true);
                          
                          				// Prevent subsequent finalization of this Object. This is not needed 
                          				// because managed and unmanaged res
                          
                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #26

                          MSBassSinger wrote:

                          That comes across as more religion than a rational approach.

                          Says the person trying to convince me that a using block swallows exceptions, whilst showing no evidence of it doing so. :doh:

                          MSBassSinger wrote:

                          The unit test for the "using" statement took 28 ms, while the standard use took 18 ms.

                          So code with different behaviour, compiled in a debug build, has approximately 10ms difference in your tests? Aside from the issue of micro-optimisation - any extra overhead from the using construct will be dwarfed by the cost of your real code - a unit test is not suitable for micro-benchmarks. You need to "warm up" your code, then measure performance over thousands of runs to get a meaningful result. Try using BenchmarkDotNet[^] to measure the code instead. And while you're at it, fix your tests so that you're comparing the same - or at least comparable - code. Change your "standard" test to:

                          public void StandardUsageTest()
                          {
                          try
                          {
                          DisposableClass test = null;
                          try
                          {
                          test = new();
                          try
                          {
                          String result = test.WhoIsIt();
                          }
                          catch (Exception ex)
                          {
                          Assert.Fail($"INNER: {ex.Message}");
                          }
                          }
                          finally
                          {
                          test?.Dispose();
                          }
                          }
                          catch (Exception exOuter)
                          {
                          Assert.Fail($"OUTER: {exOuter.Message}");
                          }
                          }


                          "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

                          M 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            Kind of. The C# script would be compiled and executed without generating an .EXE on disk. It would all be in-memory. At least, that's the plan. The "script" support for previous C# versions sucks as I recall. It's been a few years since I've dabbled with it.

                            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                            Dave Kreskowiak

                            P Offline
                            P Offline
                            Phil Benson
                            wrote on last edited by
                            #27

                            Hmm... I'm pretty sure that AV solutions will go berserk on that....

                            Who the f*** is General Failure, and why is he reading my harddisk?

                            Who the F*** is general failure and why is he reading my hard drive?

                            D 1 Reply Last reply
                            0
                            • P Phil Benson

                              Hmm... I'm pretty sure that AV solutions will go berserk on that....

                              Who the f*** is General Failure, and why is he reading my harddisk?

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #28

                              Hehe. Already handled.

                              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                              Dave Kreskowiak

                              P 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Hehe. Already handled.

                                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                Dave Kreskowiak

                                P Offline
                                P Offline
                                Phil Benson
                                wrote on last edited by
                                #29

                                now I'm curious... don't tell me you provide documentation for exceptions / custom rules :^)

                                Who the f*** is General Failure, and why is he reading my harddisk?

                                Who the F*** is general failure and why is he reading my hard drive?

                                D 1 Reply Last reply
                                0
                                • P Phil Benson

                                  now I'm curious... don't tell me you provide documentation for exceptions / custom rules :^)

                                  Who the f*** is General Failure, and why is he reading my harddisk?

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #30

                                  I wish I could, but I've said as much as I can. You know how it goes.

                                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                  Dave Kreskowiak

                                  P 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    I wish I could, but I've said as much as I can. You know how it goes.

                                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                    Dave Kreskowiak

                                    P Offline
                                    P Offline
                                    Phil Benson
                                    wrote on last edited by
                                    #31

                                    *NOW* I am curious..... that sounds a bit dirty and a bit black arts..... ;P

                                    Who the f*** is General Failure, and why is he reading my harddisk?

                                    Who the F*** is general failure and why is he reading my hard drive?

                                    D 1 Reply Last reply
                                    0
                                    • P Phil Benson

                                      *NOW* I am curious..... that sounds a bit dirty and a bit black arts..... ;P

                                      Who the f*** is General Failure, and why is he reading my harddisk?

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #32

                                      :-O :-\

                                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                      Dave Kreskowiak

                                      P 1 Reply Last reply
                                      0
                                      • D Dave Kreskowiak

                                        :-O :-\

                                        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                        Dave Kreskowiak

                                        P Offline
                                        P Offline
                                        Phil Benson
                                        wrote on last edited by
                                        #33

                                        Seriously, I'm going to have to do a bit of research as my present job involves *preventing* that kind of thing.... :wtf:

                                        Who the f*** is General Failure, and why is he reading my harddisk?

                                        Who the F*** is general failure and why is he reading my hard drive?

                                        1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          MSBassSinger wrote:

                                          That comes across as more religion than a rational approach.

                                          Says the person trying to convince me that a using block swallows exceptions, whilst showing no evidence of it doing so. :doh:

                                          MSBassSinger wrote:

                                          The unit test for the "using" statement took 28 ms, while the standard use took 18 ms.

                                          So code with different behaviour, compiled in a debug build, has approximately 10ms difference in your tests? Aside from the issue of micro-optimisation - any extra overhead from the using construct will be dwarfed by the cost of your real code - a unit test is not suitable for micro-benchmarks. You need to "warm up" your code, then measure performance over thousands of runs to get a meaningful result. Try using BenchmarkDotNet[^] to measure the code instead. And while you're at it, fix your tests so that you're comparing the same - or at least comparable - code. Change your "standard" test to:

                                          public void StandardUsageTest()
                                          {
                                          try
                                          {
                                          DisposableClass test = null;
                                          try
                                          {
                                          test = new();
                                          try
                                          {
                                          String result = test.WhoIsIt();
                                          }
                                          catch (Exception ex)
                                          {
                                          Assert.Fail($"INNER: {ex.Message}");
                                          }
                                          }
                                          finally
                                          {
                                          test?.Dispose();
                                          }
                                          }
                                          catch (Exception exOuter)
                                          {
                                          Assert.Fail($"OUTER: {exOuter.Message}");
                                          }
                                          }


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

                                          M Offline
                                          M Offline
                                          MSBassSinger
                                          wrote on last edited by
                                          #34

                                          I am sorry you do not fully understand what I wrote. First, I did show that constructor exceptions are "swallowed", though I do admit that is a subjective term. To be more precise, constructor exceptions occur outside the MSIL exception handling. In order to catch the constructor exception, the "using" has to be wrapped in a try-catch of its own. Second, because of what I just explained, the unit test that implements "using" needs the outer try-catch, while the other approach does not. The "using" approach results in nested try-catches, which is why it creates more lines of code to be executed. The coding for the unit tests is correct "as-is". The outer try-catch in your version of the StandardUsageTest() method is unnecessary. Third, the execution time is not the point. I ran them several times, to account for any caching, and the time relationship remains the same. The point is that a very popular C# coding shortcut results in more lines of code to be executed (MSIL) than the other approach, and forecloses the opportunity to capture runtime values in the exception's Data dictionary within the MSIL code. If you want to stick with the "using" statement, then do so. I am not trying to tell you or anyone else what they should do. I am presenting information for the "what" and "how" so that an objective person can decide for themselves. I provided the details, as was asked, and it completely and rationally supports my thesis. If you agree, fine. If you disagree with rational reasons you can explain, then educate us all. But since you are disagreeing without a rational basis, that is fine. You should continue to do things the way you think is best. But if you don't like my approach, just admit it is a personal preference to stick with the "using" statement. For example, if benefitting from more advanced exception handling does not provide a value for you, then implementing the "using" statement makes sense.

                                          Richard DeemingR P 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