C# 正則表達式,Delegates 與 Exception

正則表達式用來處理複雜的String正則表達式

 1 using System;
 2 using System.Text;
 3 using System.Text.RegularExpressions;
 4 public class Tester
 5 {
 6     static void Main()
 7     {
 8         string s1 = "One,Two,Three Liberty Associates, Inc.";
 9         Regex theRegex = new Regex(" |, |,");
10         StringBuilder sBuilder = new StringBuilder(); int id = 1;
11         foreach (string subString in theRegex.Split(s1))
12         {
13             sBuilder.AppendFormat("{0}: {1}\n", id++, subString);
14         }
15         Console.WriteLine("{0}", sBuilder);
16     }
17 }

 

delegates 相似C++的函數指針函數

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Text.RegularExpressions;
 7 
 8 namespace ConsoleApplication8
 9 {
10     delegate int Sample(int x, int y);  //定義delegate方法
11     class Class2
12     {
13         public int method(int x, int y)
14         { return x * y; }
15     }
16     class Class1
17     {
18         static void Main(string[] args)
19         {
20             Class2 instance = new Class2();
21             Sample mySample                         // 調用delegate的構造方法
22                    = new Sample(instance.method);   // 生成mySample實體
23             int result = mySample(2, 3);
24             Console.WriteLine(result);          // 輸出結果 6
25         }
26     }
27 }

Exceptionsui

throw exception後,throw後的代碼不執行spa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ConsoleApplication8
{
    public class Test
    {
     public static void Main()
     {
        Console.WriteLine("Enter Main...");
       Test t = new Test();
       t.Func1();
       Console.WriteLine("Exit Main...");   //未執行
     }
     public void Func1()
     {
         Console.WriteLine("Enter Func1...");
         Func2();
         Console.WriteLine("Exit Func1...");  //未執行
     }
        public void Func2()
    {
        Console.WriteLine("Enter Func2...");
         throw new System.ApplicationException();   //拋出 
         Console.WriteLine("Exit Func2...");        // 未執行
     }
    }
    
}

只在主函數中 catch 異常。指針

相關文章
相關標籤/搜索