改进c# 代码的五个技巧(一)

这篇文章主要介绍了改进c# 代码的五个技巧(一),帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

亲爱的读者,在这篇文章中,我提供了一些c#编程的最佳实践。

你是否在用户输入验证中使用异常处理机制?

如果是,那么你就是那个把你的项目执行速度降低了62倍的人。你不相信我吗?等几分钟;我来教你怎么做。但是在这个例子之前,让我们了解一下在什么地方需要异常处理。

例如,你正在验证用户的数据,对于任何无效的输入,你将引发一个异常并将其抛出给客户端,如下所示:


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

亲爱的朋友,在下一个示例中,如果你看到输出屏幕,你将意识到这种做法有多糟糕。让我们看看下面的代码。


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(); 
    } 
  } 
}

这就是你等待的输出。


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(); 
    } 
  } 
}

这是输出屏幕。


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(); 
    } 
  } 
}

输出的截图如下:


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(); 
    } 
  } 
}

输出在这里:


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(); 
    } 
  } 
}

下面是屏幕输出:

在这里,我想在输出窗口中打印一条消息,首先,我在一个静态函数中实现了它,并通过类名调用它,第二次我只是在主函数中编写它。可以,通过Console.Writeline()非常简单。输出屏幕显示单行执行比函数快9倍。因此,唯一的结论是“在盲目执行某个功能之前,试着了解情况并做出最佳决策”

结论

谢谢你能容忍我这么长时间。我在我的笔记本电脑上做了上面的测试,我的笔记本电脑有core i3处理器,4GB内存和Windows环境,在程序稳定后以释放模式输出。如果你使用不同的平台和不同的输出,在评论部分有足够的空间写评论。

以上就是改进c# 代码的五个技巧(一)的详细内容,更多关于改进c# 代码的资料请关注得得之家其它相关文章!

本文标题为:改进c# 代码的五个技巧(一)