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