C# 控制台应用程序 XML 注释和内联注释



嗨,我是编程新手,非常困惑如何进行 XML 注释和内联注释,我没有编程背景,2 周前刚开始。它只是简单的控制台应用程序计算器。你们能检查一下我的XML注释和内联注释是否合适和正式吗?

        /// <summary>
    /// Method does:Perform LengthCalculator 
    /// <paramref name="LengthCalculatorOption"/>Determine what is user's input 
    /// <paramref name="AnotherConversion"/> Do while loop condition 
    /// return value: LengthCalculatorOption
    /// </summary>
    /// Pre:  Main menu option input = 1 (Length Calculator) 
    /// Post: Perform Length Calculator
    static int LengthCalculator() {
        int LengthCalculatorOption;
        string AnotherConversion = null;
        do {
            LengthCalculatorMenu(); // Call LenthCalculatorMenu() to display Length Calculator Menu
            LengthCalculatorOption = ValidLengthCalculatorReadOption(); // Call ValidLengthCalculatorReadOption to read the Valid Length Calculator Option
            if (LengthCalculatorOption == 1) { // Do this code if LengthCalculatorOption input == 1 
                ConvertCentimetresToFeetAndInches(); // Call ConvertCentimetresToFeetAndInches() to Convert Centimetres To Feet And Inches
                Console.Write("nWould you like to make an another Length conversion?" // Ask user if they want to do Another Length Conversion
                             + "nn(Enter [Y] to make an another conversion/Enter [N] return to Main menu):");
                AnotherConversion = Console.ReadLine(); // Read AnotherConversion Input
            } else if (LengthCalculatorOption == 2) { // Do this code if LengthCalculatorOption input == 2
                ConvertFeetAndInchesToCentimetres(); // Call ConvertFeetAndInchesToCentimetres() to Convert feet and inches to centimetres
                Console.Write("nWould you like to make an another Length conversion?"
                             + "nn(Enter [Y] to make an another conversion/Enter [N] return to Main menu):");
                AnotherConversion = Console.ReadLine(); // Read AnotherConversion Input
            } else if (LengthCalculatorOption == 3) { // Do this code if LengthCalculatorOption input == 3
                Main(); // Call Main() to return to Main Menu
            } //End if
        } while (AnotherConversion == "Y" || AnotherConversion == "y");// End While , If AnotherConversion = "Y" OR "y", start the do while loop again
        return LengthCalculatorOption;
    }//End LenthCalculator
    //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// Method: Convert Centimetres To Feet And Inches
    /// <paramref name="Centimetres"/> Get the centimetres that user want to conver 
    /// <paramref name="Feet"/> For convertsion Calculation 
    /// <paramref name="Inches"/> For convertsion Calculation 
    /// <paramref name="TotalInches"/> For convertsion Calculation 
    /// <paramref name="CENTIMETRES_PER_INCH"/> Centimetres per inch = 2.54, 
    /// <paramref name="INCHES_PER_FOOT"/> Inches per foot = 12,
    /// return value:
    /// </summary>
    /// Pre: LengthCalculatorOption == 1
    /// Post: Do ConvertCentimetresToFeetAndInches()
    static void ConvertCentimetresToFeetAndInches() {
        double Centimetres = 0.0, Feet = 0.0, Inches = 0.0, TotalInches = 0.0;
        const double CENTIMETRES_PER_INCH = 2.54, INCHES_PER_FOOT = 12;
        Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches:");
        Centimetres = double.Parse(Console.ReadLine()); //Read the Centimetres enter by user 
        TotalInches = (Centimetres / CENTIMETRES_PER_INCH); // This will take a floor function of Centimetres/2.54
        Feet = (TotalInches - TotalInches % INCHES_PER_FOOT) / INCHES_PER_FOOT; // This will make it divisible by 12
        Inches = TotalInches % INCHES_PER_FOOT; // This will give you the remainder after you divide by 12
        Console.WriteLine("nThe equivalent in feet and inches is {0} ft {1} ins", Feet, Inches);
    }// End ConvertCentimetresToFeetAndInches
    //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// Method: To Convert Feet and Inches to Centimetres
    /// <paramref name="Centimetres"/> For Conversion Calculation
    /// <paramref name="Feet"/> Get the Feet that user want to conver 
    /// <paramref name="Inches"/> Get the Inches that user want to conver 
    /// <paramref name="CENTIMETRES_PER_INCH"/> Centimetres per inch = 2.54 For Conversion Calculation
    /// <paramref name="INCHES_PER_FOOT"/> Inches per foot = 12  For Conversion Calculation
    /// </summary>
    /// Pre:  LengthCalculatorOption == 2
    /// Post: Do ConvertFeetAndInchesToCentimetres()
    static void ConvertFeetAndInchesToCentimetres() {
        int Feet, Inches; 
        double Centimetres;
        const double CENTIMETRES_PER_INCH = 2.54, INCHES_PER_FOOT = 12;
        Console.WriteLine("Please Enter the Feet:");
        Feet = int.Parse(Console.ReadLine()); // Get the Feet that user want to conver 
        Console.WriteLine("Please Enter the Inches:");
        Inches = int.Parse(Console.ReadLine()); // Get the Inches that user want to conver 
        Centimetres = ((Feet * INCHES_PER_FOOT) + Inches) * CENTIMETRES_PER_INCH;
        Console.WriteLine("The equivalent in centimetres is {0}cm", Centimetres);
    }//End ConvertFeetAndInchesToCentimetres
    ///--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// Method: Display the sub-menu of LengthCalculatorMenu
    /// <paramref name="LengthCalculatorMenu"/> SubMenu for LengthCalculator
    /// return value:
    /// </summary>
    /// Pre:  Main menu option input = 1 (Length Calculator) 
    /// Post: LengthCalculatorMenu Displayed
    static void LengthCalculatorMenu() {
        string LengthCalculatorMenu = ("Enter 1) Convert Centimetres to Feet and Inches:"
                                    + "nEnter 2) Convert feet and inches to centimetres:"
                                    + "nEnter 3) Cancel this Option And Return to Main Menu:");
        Console.Write(LengthCalculatorMenu);
    }//End LengthCalculatorMenu
    ///--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// Method: Allow user to enter valid input for Length Calculator
    /// <paramref name="LengthCalculatorOption"/> Read the input for user's option 
    /// <paramref name="ValidLengthCalculatorOption"/> Determine the validation of input for user's option
    /// </summary>
    /// Pre: ??
    /// Post: ??
    static int ValidLengthCalculatorReadOption() {
        int LengthCalculatorOption;
        bool ValidLengthCalculatorOption = false;
        do {
            LengthCalculatorOption = int.Parse(Console.ReadLine());
            if ((LengthCalculatorOption >= 1) && (LengthCalculatorOption <= 3)) {
                ValidLengthCalculatorOption = true;
            } else {
                ValidLengthCalculatorOption = false;
            } // end if
            if (!ValidLengthCalculatorOption) {
                Console.WriteLine("nt Option must be 1, 2 or 3. Please Re-Enter your Option");
                LengthCalculatorMenu();
            } //end if 
        } while (!ValidLengthCalculatorOption); //End While 
        return LengthCalculatorOption;
    }// End LengthCalculatorReadOption
    ///--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

节省大量时间并删除所有评论,除了这些

/// <summary>
/// Method: Allow user to enter valid input for Length Calculator
/// <paramref name="LengthCalculatorOption"/> Read the input for user's option 
/// <paramref name="ValidLengthCalculatorOption"/> Determine the validation of input for user's option
/// </summary>

你也不需要prepost的东西。<summary>应如下所示:

/// <summary>
/// Allow user to enter valid input for Length Calculator.
/// </summary>
/// <paramref name="LengthCalculatorOption"/> Read the input for user's option 
/// <paramref name="ValidLengthCalculatorOption"/> Determine the validation of input for user's option

摘要中不需要Method这个词。保持最少的评论,因为这有助于任何需要知道该方法功能的人的可读性。

不要为代码中的注释而烦恼,除非它们确实提供了价值。过多的注释会使阅读代码更加困难,而不是更容易。

编辑:

只是看了我写的XML注释,注意到我使用了<param>而不是<paramref>例如

/// <summary>
/// Blah, blah.
/// </summary>
/// <param name="targetSiteId">The target site identifier.</param>

如果您在注释的另一部分中引用参数,则应使用 <paramref>,例如

/// <summary>
/// Blah, blah.
/// </summary>
/// <param name="targetSiteId">The target site identifier.</param>
/// <remarks>Note that the <paramref name="targetSiteID" /> should be non zero.</remarks>

最新更新