Parse and TryParse
The Parse(string s) and TryParse(string s, out datetime) in DateTime are used to convert the date time of the character type to an equivalent system.datetime. So, there is no difference between them, except the parameters of the function are different. First look at the code:
String datetimestr = ""; DateTime dt = DateTime.Parse (DATETIMESTR);
Run an empty string, convert it to datetime, obviously not, and parse () throws an exception: system.formatexception:s does not contain a valid string representation of the date and time . However, run TryParse This conversion method:
String datetimestr = ""; DateTime DT2; DT2 is passed to the function TryParse () bool Sucflag = Datetime.tryparse (datetimestr, out DT2) without initialization;
The conversion is first non-throwing exception, DT2 is assigned to the minimum value of datetime, Sucflag is false. Take a look at the comment on the function:
When this method returns, contains the System.DateTime value equivalent to the date and time contained in S, if the conversion succeeded, or System.DateTime.MinValue if the conversion failed. If the s parameter is null, is an empty string (""), or does not contain a valid string representation of the date and time, the conversion fails. * This parameter is passed without initialization. This function does not throw any exceptions.
Try-parse
After seeing their differences, further, parse () throws an exception that inevitably affects performance, TryParse () does not throw any exceptions, which is a design pattern that optimizes abnormal performance, called try-parse pattern. Here is the official explanation for Microsoft:
For extremely performance-sensitive APIs, a even faster pattern than the tester-doer pattern described in the previous SE Ction should be used. The pattern calls for adjusting the member name to make a well-defined test case a part of the member semantics. For example, DateTime defines a, Parse method, the throws an exception if parsing of a string fails. It also defines a corresponding TryParse method that attempts to parse, but returns false if parsing is unsuccessful and R Eturns The result of a successful parsing using an out parameter.
Tester-doer
In explaining the Try-parse model, Microsoft proposed another mode:tester-doer mode , what is tester-doer mode? The write exception in the function degrades performance, and Microsoft gives this pattern to reduce the side effects of the exception.
The following code:
Icollection<int> numbers = omits logical numbers to get data. ADD (1);//add do not write a writable check here
Above flaw: If the collection is read-only, the method add throws an exception. Calling this method often throws an exception, which can affect the performance of the system. To avoid this design flaw, Microsoft proposed: Sometimes performance of an exception-throwing member can is improved by breaking the member into.
Break Add () to:
Icollection<int> numbers = omit logical if (!numbers) to get data. isreadonly)//tester{ numbers. ADD (1); Doer}
Tester-doer Model Summary:
The member used to test a condition, which in we example are the property isreadonly, are referred to as The tester. The member used to perform a potentially throwing operation, the Add method in we example, is referred to as the Doer.
After decomposition, read-only detection is done, which reduces the number of read-only exceptions thrown by add and improves performance.
Summarize
Try-parse pattern and Tester-doer mode are two kinds of optimization methods to replace parabolic anomalies, which play an important role in optimizing design performance.
The above is through the content of the parse and TryParse Try-parse and Tester-doer modes, please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content!