C # Insights and Experience (iii) Regex,exception,delegate & Events

Source: Internet
Author: User

One, Regular Expression

Using system;using system.collections.generic;using system.linq;using system.text;using System.text.regularexpressions;using system.threading.tasks;namespace regextest{    class program    {        static void Main (string[] args)        {            string string1 = "This is a test string";            Regex thereg = new Regex (@ "(\s+) \s");            MatchCollection thematches = thereg.matches (string1);            foreach (Match thematch in thematches) {                Console.WriteLine ("Thematch.length: {0}", thematch.length);                if (thematch.length! = 0)                    Console.WriteLine ("Thematch: {0}", thematch.tostring ());}}}    

Use regular expressions for pattern matching, for example, find (\s+) \s is a word, use @ To prevent translation.

Note Using System.Text.RegularExpressions;

Match () Returns an object that matches returns all conforming objects.

. any single character char
* greater than or equal to 0 arbitrary characters
[a-f] any single character between a-f
^a A string that starts at any a
z$ any z-terminated string

Second, Exception

Using try or throw throws exception catch handling exceptions, some exceptions can do nothing, like endofstreamexception. Exceptions are not vulnerabilities or errors, and handling of exceptions is important, and exceptions affect the performance of the program.

try {Double A = 5; Double b = 0; Dodivide (A, b);}

catch (System.DivideByZeroException)

{...}

catch (System.ArithmeticException)

{...}

catch (Exception e)

{...}

Public double dodivide (double A, double b)

{

if (b = = 0) {throw new System.DivideByZeroException ();}

if (a = = 0) {throw new System.ArithmeticException ();}

return a/b;

}

Finally

{

...//regardless of whether the exception occurred, the code here will be executed

}

The statements in the finally are always executed in order to catch and perform the corresponding actions.

Iii. Delegates & Events

The delegate is similar to the previous learning pointers, which can be encapsulated to make the method easier to call.

delegate int Demo (int a, int b);

Demo test = new Demo (class1.function ());

Test (from);

Anonymous:

Demo test = delegate(int a, int b) {return (a = = B);};

C # Insights and Experience (iii) Regex,exception,delegate & Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.