技巧1 自動生成帶參構造函數
當我們在編寫代碼時會經常遇到初始化一個的類,需要通過構造函數進行對象初始化。那么這個時候我們可能會需要逐個去手動寫,這樣的工作即重復又無趣。如果是在項目非常緊急的情況下還有大量的字段需要與入參一一對應起來簡直太要命了。大致情況如下:
public class Configinfo : Entity{public Configinfo() { }public Configinfo(int appType, string appName, string appSecretKey, string clientVersion, string updateUrl, string updateLogUrl, string installPath, string mainUpdateUrl, string mainAppName){AppType = appType;AppName = appName ?? throw new ArgumentNullException(nameof(appName));AppSecretKey = appSecretKey ?? throw new ArgumentNullException(nameof(appSecretKey));ClientVersion = clientVersion ?? throw new ArgumentNullException(nameof(clientVersion));UpdateUrl = updateUrl ?? throw new ArgumentNullException(nameof(updateUrl));UpdateLogUrl = updateLogUrl ?? throw new ArgumentNullException(nameof(updateLogUrl));InstallPath = installPath ?? throw new ArgumentNullException(nameof(installPath));MainUpdateUrl = mainUpdateUrl ?? throw new ArgumentNullException(nameof(mainUpdateUrl));MainAppName = mainAppName ?? throw new ArgumentNullException(nameof(mainAppName));}/// <summary>/// 1:ClientApp 2:UpdateApp/// </summary>public int AppType { get; set; }/// <summary>/// Need to start the name of the app./// </summary>public string AppName { get; set; }/// <summary>/// application key/// </summary>public string AppSecretKey { get; set; }/// <summary>/// Client current version./// </summary>public string ClientVersion { get; set; }/// <summary>/// Update check api address./// </summary>public string UpdateUrl { get; set; }/// <summary>/// Update log web address./// </summary>public string UpdateLogUrl { get; set; }/// <summary>/// installation path (for update file logic)./// </summary>public string InstallPath { get; set; }/// <summary>/// Update check api address./// </summary>public string MainUpdateUrl { get; set; }public string MainAppName { get; set; }}
看起來是不是非常頭疼,那么如何解決這個問題呢?可以通過VisualStudio幫助開發者進行這重復的工作。
空白處點擊一下,會出現一個小工具圖標
點擊小工具,選擇生成構造函數。
選擇需要作為構造函數的參數
生成代碼如下
public Configinfo(int appType, string appName, string appSecretKey, string clientVersion, string updateUrl, string updateLogUrl, string installPath, string mainUpdateUrl, string mainAppName){AppType = appType;AppName = appName ?? throw new ArgumentNullException(nameof(appName));AppSecretKey = appSecretKey ?? throw new ArgumentNullException(nameof(appSecretKey));ClientVersion = clientVersion ?? throw new ArgumentNullException(nameof(clientVersion));UpdateUrl = updateUrl ?? throw new ArgumentNullException(nameof(updateUrl));UpdateLogUrl = updateLogUrl ?? throw new ArgumentNullException(nameof(updateLogUrl));InstallPath = installPath ?? throw new ArgumentNullException(nameof(installPath));MainUpdateUrl = mainUpdateUrl ?? throw new ArgumentNullException(nameof(mainUpdateUrl));MainAppName = mainAppName ?? throw new ArgumentNullException(nameof(mainAppName));}
技巧2 Debug調試根據堆棧進行查找到代碼調用
在調式中我們通常都是按F10或者F11進行調試,如果代碼數量較少那么調試起來是非常簡單的。如果代碼多或者代碼中方法內部不會集中很多其他的方法,這時候我們往往會忘記上一步是由哪個地方跳轉過來的從而導致我們暈頭轉向,這個時候我們就需要利用VisualStudio中的堆棧信息進行輔助幫助我們思路清晰的查看代碼的調用鏈路。
示例代碼如下:
internal class Class1{public void Test() {var local = PublicMethod(new Juster(18));Console.WriteLine(local);}public static double PublicMethod(Juster juster) {double result = GetScale(in juster);return result + result;}private static double GetScale(in Juster input) => input.Age * input.Age;}
調用代碼:
static void Main(string[] args){Class1 class1 = new Class1();class1.Test();Console.WriteLine();}
這個時候假設我們需要調試class1中的三個方法,打上三個斷點。
這個時候我們運行到最后一個方法時,假設代碼內容非常復雜這個時候我們已經暈了。這時候就需要打開“堆棧調用”的窗口,查看具體的調試信息。
然后根據堆棧信息逐步往后看。我們從下往上逐步雙擊堆棧信息,VisualStudio會自動幫助我們把關注點跳轉到對應的代碼行數也就是“發生地”。