透過Parse和TryParse Try-Parse和Tester-Doer模式
Parse和TryParse
DateTime中Parse(string s)和TryParse(string s, out datetime)都是用來將字元型的日期時間轉換為等效的System.DateTime。那麼,他們之間有沒有差別呢,除了函數的參數不同。先看下程式碼:
string dateTimeStr = ""; DateTime dt = DateTime.Parse(dateTimeStr);
運行空字串,將其轉換為日期時間型,顯然不能轉化,並且Parse()會拋出一個異常: System.FormatException: s 中不包含日期和時間的有效字串表示形式。但是,執行TryParse這個轉換方法:
string dateTimeStr = ""; DateTime dt2; //dt2未经初始化,就被传递给函数TryParse() bool sucflag = DateTime.TryParse(dateTimeStr, out dt2);
轉換首先是不拋出例外的,dt2被賦值為日期時間的最小值,sucflag為false。請參閱函數的註解:
當此方法傳回時,如果轉換成功,則包含與s 中包含的日期和時間等效的System.DateTime 值;如果轉換失敗,則為System.DateTime.MinValue。如果s 參數為 null,是空字串 (“”) 或不包含日期和時間的有效字串表示形式,則轉換失敗。 *該參數未經初始化即傳遞。這個函數是不會拋出任何異常的。
Try-Parse
看到他們的不同後,進一步來講,parse()拋出異常必然影響性能,TryParse()未拋出任何異常,這是優化例外效能的設計模式,稱為Try-Parse Pattern。以下是微軟的官方解釋:
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 parse, but returns false if parsing is unsuccessful and returns the result of a successful parsing using an out parameter.
Tester-Doer
ry ##T#Tester-Doer
##ry-Par#T
#se模式時,微軟提出了另一個模式:
,什麼是Tester-Doer模式呢? 函數中寫入異常,會降低效能,微軟給了這種模式來減少異常所帶來的副作用。
如下程式碼:
ICollection<int> numbers = 省略获取数据的逻辑 numbers.Add(1);//Add此处不做可写性检查
以上缺陷:假如集合是唯讀的,方法Add會拋出例外。呼叫這個方法的地方會經常拋出異常,因此會影響系統的效能。為了避免這個設計缺陷,微軟提出: Sometimes performance of an exception-throwing member can be improved by breaking the member into two.
將Add()分解為:# 將Add()分解為:rrreee Tester-Do Testerer Testerer Testerer Testerer Testerer Testerer Testerer模式總結:The member used to test a conditionICollection<int> numbers = 省略获取数据的逻辑if(!numbers.IsReadOnly) //Tester{ numbers.Add(1); //Doer}登入後複製, which in our example is the property
IsReadOnly, is referred to as the tester. The member used to perform a
, the Add method in our example, is referred to as the doer.
potentially throwing operation# 分解後,先做唯讀性檢測,這樣會減少Add拋出只讀性異常的次數,提升效能。
Try-Parse Pattern和Tester-Doer模式是兩種替代拋異常的最佳化方式,起到優化設計效能的作用。 ###### 以上就是透過Parse和TryParse Try-Parse和Tester-Doer模式 的內容,更多相關內容請關注PHP中文網(www.php.cn)! ################
總結

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。
