In this article, dear reader, I’ve provided some C # programming best practices.

Do you use exception handling in user input validation?

If so, you are the one who has slowed your project execution by a factor of 62. Don’t you believe me? Wait a few minutes; I’ll show you how to do it. But before this example, let’s look at where exception handling is needed.

For example, if you are validating user data, for any invalid input, you will throw an exception to the client, like this:

class BusinessLogcCheck { public void Check() { try { //Your validation code is here } catch (Exception ex) { throw new Exception("My own exception"); }}}

Dear friends, in the next example, if you see the output screen, you will realize how bad this is. Let’s look at the code below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; namespace Test1 { class Program { public static void ThrowTest() { throw new Exception("This is exceptopn"); } public static Boolean Return() { return false; } static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); try { ThrowTest(); } catch { } sw.Stop(); Console.WriteLine("With Exception " + sw.ElapsedTicks); sw.Restart(); try { Return(); } catch { } sw.Stop(); Console.WriteLine("With Return " + sw.ElapsedTicks); Console.ReadLine(); }}}

This is the output you’re waiting for.

My proof of concept is very simple. In one function, I threw an exception, and in the other function, I returned a Boolean value after checking user input. I have also attached a calculator screen (haha..) “To convince you how exception handling affects code performance.

Therefore, we can conclude, “Do not throw an exception for user input validation.” Use the Boolean return technique (or similar technique) to validate the input in the business logic “. Because the overhead of exception objects is very high.

Never try-Catch in a loop

Yes, it also has to do with exception handling. I repeat “never try-catch in a loop”. Let me give you an example.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; namespace Test1 { class Program { static void Method1() { for (int i = 0; i < 1000; i++) { try { int value = i * 100; if (value == -1) { throw new Exception(); } } catch { } } } static void Method2() { try { for (int i = 0; i < 1000; i++) { int value = i * 100; if (value == -1) { throw new Exception(); } } } catch { } } static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); Method1(); sw.Stop(); Console.WriteLine("Within Loop " + sw.ElapsedTicks); sw.Restart(); Method2(); sw.Stop(); Console.WriteLine("Outside of Loop " + sw.ElapsedTicks); Console.ReadLine(); }}}

This is the output screen.

In this program on method1, I implement exception handling in a for loop, and in method2, I implement exception handling without a loop. Our output window indicates that if we implement a try-catch outside the for loop, the program will execute twice as fast as a try-catch inside the loop.

Again, the only conclusion is “Do not implement try-catch in the project loop. (yes! Not just in a for loop, but in any loop.)

Are you crazy enough to use the new operator to create an integer variable?

Dear reader, don’t criticize me for writing such a long title, and don’t use the new operator to create a simple integer variable. I know you will say that if you use the new operator to create a simple integer variable it will be automatically set to 0 and will not suffer from errors such as “unassigned local variable”, but this is really needed to get an automatic assignment to 0. Is your purpose to create a local variable to store? Let’s take a look at how the new operator can degrade the performance of code execution.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; namespace Test1 { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { int a = new int(); a = 100; } sw.Stop(); Console.WriteLine("Using New operator:- " + sw.ElapsedTicks); sw.Restart(); for (int i = 0; i < 1000; i++) { int a; a = 100; } sw.Stop(); Console.WriteLine("Without new operator:- "+ sw.ElapsedTicks); Console.ReadLine(); }}}

The output screenshot is as follows:

The new operator slows execution by a factor of five. I can deny the output screen but for one thing!! You have to create 1000 variables at a time; In our project, instead of creating 1000 variables at a time, we will create at most two or three.

All right. Is your application a Web application? If so, then please check the number of hits for any popular web application, I’m sure it’s over 1000 per day.

Again, the conclusion of this line is “Don’t be crazy about using the new operator to create integer variables.”

Choose the best set for your purpose

We.NET developers are familiar with collections in C # and the way they store values. Let’s take a look at how they perform searches. View the performance of searching for integers. This is my code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; namespace Test1 { class Program { static void Main(string[] args) { List<Int32> li = new List<Int32>(1000); Dictionary<int, int> di = new Dictionary<int, int>(1000); int[] arr = new int[1000]; int a; for (int i = 0; i < 1000; i++) { li.Add(i); di.Add(i, i); arr[i] = i; } Stopwatch sw = new Stopwatch(); sw.Start(); a = li[500]; sw.Stop(); Console.WriteLine("From list:- " + sw.ElapsedTicks); sw.Start(); a = arr[500]; sw.Stop(); Console.WriteLine("From Integer array:- " + sw.ElapsedTicks); sw.Restart(); a = di[500]; sw.Stop(); Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks); Console.ReadLine(); }}}

The output is here:



We can clearly see that in the case of dictionaries, search performance is the worst, while in the case of lists and integer arrays, performance is very similar.

The method is good, but not all the time

If you remember from your first few days of learning programming, you learned the concept of always implementing a method to implement good practices in code, and yes, it’s good to implement a method to perform certain tasks. Methods have thousands of advantages in programming, but let’s take a look at how methods can degrade execution performance. Again, this is not to argue against methods, but simply to show that method invocation is an expensive mechanism and to provide ideas about where to implement methods and where not to implement methods. Let’s look at the code below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; namespace Test1 { class test { public static void Print() { Console.WriteLine("I am function from Class"); } } class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); test.Print(); sw.Stop(); Console.WriteLine(sw.ElapsedTicks); sw.Restart(); Console.WriteLine("I am single statement within main"); sw.Stop(); Console.WriteLine(sw.ElapsedTicks); Console.ReadLine(); }}}

Here is the screen output:

In this case, I want to print a message in the output window. First, I implement it in a static function and call it by the class name, and the second time I just write it in the main function. Yes, it’s easy with console.writeLine (). The output screen shows that single-line execution is 9 times faster than a function. Therefore, the only conclusion is to “try to understand the situation and make the best decision before blindly implementing a feature”

conclusion

Thank you for putting up with me so long. I did the above tests on my laptop, which has a Core i3 processor, 4GB of RAM and a Windows environment, and outputs in release mode after the program stabilises. If you are using different platforms and different outputs, there is plenty of space in the comments section to write a comment.

Welcome to pay attention to my public number, if you have a favorite foreign language technical articles, you can pass the public number message recommended to me.