简体   繁体   English

帮助我理解这个 c# 代码

[英]Help me to understand this c# code

this code in Beginning C# 3.0: An Introduction to Object Oriented Programming这段代码在开始 C# 3.0: An Introduction to Object Oriented Programming

this is a program that has the user enter a couple of sentences in a multi - line textbox and then count how many times each letter occurs in that text这是一个程序,它让用户在多行文本框中输入几个句子,然后计算每个字母在该文本中出现的次数

  private const int MAXLETTERS = 26;            // Symbolic constants
  private const int MAXCHARS = MAXLETTERS - 1;
  private const int LETTERA = 65;

......... …………

private void btnCalc_Click(object sender, EventArgs e)
  {
    char oneLetter;
    int index;
    int i;
    int length;
    int[] count = new int[MAXLETTERS];
    string input;
    string buff;

    length = txtInput.Text.Length;
    if (length == 0)    // Anything to count??
    {
      MessageBox.Show("You need to enter some text.", "Missing Input");
      txtInput.Focus();
      return;
    }
    input = txtInput.Text;
    input = input.ToUpper();


    for (i = 0; i < input.Length; i++)    // Examine all letters.
    {
      oneLetter = input[i];               // Get a character
      index = oneLetter - LETTERA;        // Make into an index
      if (index < 0 || index > MAXCHARS)  // A letter??
        continue;                         // Nope.
      count[index]++;                     // Yep.
    }

    for (i = 0; i < MAXLETTERS; i++)
    {
      buff = string.Format("{0, 4} {1,20}[{2}]", (char)(i + LETTERA)," ",count[i]);
      lstOutput.Items.Add(buff);
    }
  }

I do not understand this line我不明白这一行

 count[index]++;

and this line of code和这行代码

buff = string.Format("{0, 4} {1,20}[{2}]", (char)(i + LETTERA)," ",count[i]);

count[index]++; means "add 1 to the value in count at index index ".意思是“在索引index处的count加 1”。 The ++ is specifically known as incrementing . ++特别称为递增 What the code is doing is tallying the number of occurrences of a letter.代码所做的是计算字母出现的次数。

buff = string.Format("{0, 4} {1,20}[{2}]", (char)(i + LETTERA)," ",count[i]); is formatting a line of output.正在格式化一行 output。 With string.Format , you first pass in a format specifier that works like a template or form letter.使用string.Format ,您首先传入一个格式说明符,其作用类似于模板或套用信函。 The parts between { and } specify how the additional arguments passed into string.Format are used. {}之间的部分指定如何使用传递给string.Format的附加 arguments。 Let me break down the format specification:让我分解格式规范:

{0, 4}       The first (index 0) argument (which is the letter, in this case).
             The ,4 part means that when it is output, it should occupy 4 columns
             of text.

{1,20}       The second (index 1) argument (which is a space in this case).
             The ,20 is used to force the output to be 20 spaces instead of 1.

{2}          The third (index 2) argument (which is the count, in this case).

So when string.Format runs, (char)(i + LETTERA) is used as the first argument and is plugged into the {0} portion of the format.因此,当string.Format运行时, (char)(i + LETTERA)用作第一个参数并插入到格式的{0}部分。 " " is plugged into {1} , and count[i] is plugged into {2} . " "插入{1}count[i]插入{2}

count[index]++;

That's a post-increment .那是一个后增量 If you were to save the return of that it would be count[index] prior to the increment, but all it basically does is increment the value and return the value prior to the increment.如果要保存它的返回值,它将是 count[index] 在增量之前,但它基本上所做的只是增加值并在增量之前返回值。 As for the reason why there is a variable inside square brackets, it is referencing a value in the index of an array.至于方括号里面有变量的原因,它是在引用数组索引中的一个值。 In other words, if you wanted to talk about the fifth car on the street, you may consider something like StreetCars(5).换句话说,如果你想谈论街上的第五辆车,你可以考虑像 StreetCars(5) 这样的东西。 Well, in C# we use square brackets and zero-indexing, so we would have something like StreetCars[4].好吧,在 C# 中,我们使用方括号和零索引,所以我们会有类似 StreetCars[4] 的东西。 If you had a Car array call StreetCars you could reference the 5th Car by using the indexed value.如果您有一个 Car 数组调用 StreetCars,您可以使用索引值引用第 5 辆车。

As for the string.Format() method, check out this article .至于 string.Format() 方法,请查看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM