Home  >  Article  >  Backend Development  >  .NET Framework-The difference between Try-Parse and Tester-Doer

.NET Framework-The difference between Try-Parse and Tester-Doer

黄舟
黄舟Original
2017-03-20 11:50:491867browse

Parse and TryParse

In DateTime, Parse(string s) and TryParse(string s, out datetime) are both used to convert character date and time Converted to the equivalent System.DateTime. So, is there any difference between them, except that the parameters of the function are different. Let’s take a look at the code first:

            string dateTimeStr = "";
            DateTime dt = DateTime.Parse(dateTimeStr);

Run the empty string and convert it into a date and time type. Obviously it cannot be converted, and Parse() will throw an exception: System. FormatException: s does not contain a valid string representation of the date and time . However, running TryParse this conversion method:

            string dateTimeStr = "";       
            DateTime dt2; //dt2未经初始化,就被传递给函数TryParse()
            bool sucflag = DateTime.TryParse(dateTimeStr, out dt2);

 The conversion does not throw an exception at first, dt2 is assigned the minimum value of date and time, sucflag is false. Look at the comments on the function:

When this method returns, if the conversion is successful, a System.DateTime value equivalent to the date and time contained in s is included; System.DateTime.MinValue if conversion fails. The conversion fails if the s parameter is null, is an empty string (""), or does not contain a valid string representation of the date and time. *This parameter is passed without initialization. This function will not throw any exceptions.

Try-Parse

After seeing their differences, further speaking, exceptions thrown by parse() will inevitably affect performance. TryParse() did not throw any exceptions. This is A design pattern that optimizes abnormal performance is called Try-Parse Pattern. The following is Microsoft's official explanation:

For extremely performance-sensitive APIs, an even faster pattern than the Tester-Doer Pattern described in the previous section 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 that 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 returns the result of a successful parsing using an out parameter.

Tester-Doer

When explaining the Try-Parse mode, Microsoft proposed Another mode: Tester-Doer mode, what is Tester-Doer mode? Writing exceptions in functions will reduce performance. Microsoft has provided this mode to reduce the side effects of exceptions.

The following code:

ICollection<int> numbers = 省略获取数据的逻辑
numbers.Add(1);//Add此处不做可写性检查

The above defect: If the collection is read-only, the Add method will throw an exception. Exceptions will often be thrown where this method is called, thus affecting system performance. In order to avoid this design flaw, Microsoft proposed: Sometimes performance of an exception-throwing member can be improved by breaking the member into two.

 Decompose Add() into:

ICollection<int> numbers = 省略获取数据的逻辑if(!numbers.IsReadOnly) //Tester{
    numbers.Add(1); //Doer}

 Tester-Doer Pattern summary:

The member used to test a condition, which in our example is the property IsReadOnly, is referred to as the tester. The member used to perform a potentially throwing operation, the Add method in our example, is referred to as the doer.

After decomposition, read-only detection is done first, which will reduce Add the number of times read-only exceptions are thrown to improve performance.

Summary
Try-Parse Pattern and Tester-Doer mode are two optimization methods that replace throwing exceptions and play a role in optimizing design performance.

The above is the detailed content of .NET Framework-The difference between Try-Parse and Tester-Doer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn