0

代码示例:

using System;

public class Test {

    public static void Main() {
        int a = 0;
        if(a++ == 0){
            Console.WriteLine(a);
        }
    }
}

在这段代码中,控制台将写: 1. 我可以用另一种方式编写这段代码:

public static void Main() {
        int a = 0;
        if(a == 0){
            a++;
            Console.WriteLine(a);
        }
}

这两个示例的工作方式完全相同(根据我对 postfix 的了解)。问题在于这个来自 Microsoft 教程的示例:

using System;

public class Document {

// Class allowing to view the document as an array of words:
public class WordCollection {
    readonly Document document;

    internal WordCollection (Document d){
        document = d;
    }

    // Helper function -- search character array "text", starting
    // at character "begin", for word number "wordCount". Returns
    //false if there are less than wordCount words. Sets "start" and
    //length to the position and length of the word within text
    private bool GetWord(char[] text, int begin, int wordCount,
                         out int start, out int length) {

        int end = text.Length;
        int count = 0;
        int inWord = -1;
        start = length = 0;
        for (int i = begin; i <= end; ++i){
            bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);
            if (inWord >= 0) {
                if (!isLetter) {
                    if (count++ == wordCount) {//PROBLEM IS HERE!!!!!!!!!!!!
                        start = inWord;
                        length = i - inWord;
                        return true;
                    }
                    inWord = -1;
                }
            } else {
                if (isLetter) {
                    inWord = i;
                }
            }
        }
        return false;
    }

    //Indexer to get and set words of the containing document:
    public string this[int index] {
        get 
        {
            int start, length;
            if(GetWord(document.TextArray, 0, index, out start,
                                                     out length)) {
                return new string(document.TextArray, start, length);                                            
            } else {
                throw new IndexOutOfRangeException();
            }
        }
        set {
            int start, length;
            if(GetWord(document.TextArray, 0, index, out start,
                                                      out length))
            {
                //Replace the word at start/length with 
                // the string "value"
                if(length == value.Length){
                    Array.Copy(value.ToCharArray(), 0,
                               document.TextArray, start, length);
                }                   
                else {
                    char[] newText = new char[document.TextArray.Length +
                                              value.Length - length];
                    Array.Copy(document.TextArray, 0, newText, 0, start);
                    Array.Copy(value.ToCharArray(), 0, newText, start, value.Length);
                    Array.Copy(document.TextArray, start + length, newText, 
                               start + value.Length, document.TextArray.Length - start - length);
                    document.TextArray = newText;
                }
            } else {
                throw new IndexOutOfRangeException();
            }
        }
    }

    public int Count {
        get {
            int count = 0, start = 0, length = 0;
            while (GetWord(document.TextArray, start + length,
                    0, out start, out length)) {
                        ++count;
            }
            return count;
        }
    }
}

// Class allowing the document to be viewed like an array
// of character
public class CharacterCollection {

    readonly Document document;

    internal CharacterCollection(Document d) {
        document = d;
    }

    //Indexer to get and set character in the containing
    //document
    public char this[int index] {
        get {
            return document.TextArray[index];
        }
        set {
            document.TextArray[index] = value;
        }
    }

    //get the count of character in the containing document
    public int Count {
        get {
            return document.TextArray.Length;
        }
    }
}

//Because the types of the fields have indexers,
//these fields appear as "indexed properties":
public WordCollection Words;
public readonly CharacterCollection Characters;

private char[] TextArray;

public Document(string initialText) {
    TextArray = initialText.ToCharArray();
    Words = new WordCollection(this);
    Characters = new CharacterCollection(this);
}

public string Text {
    get {
        return new string(TextArray);
    }
}

class Test {
    static void Main() {
        Document d = new Document(
            "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"              
        );

        //Change word "peter" to "penelope"
        for(int i = 0; i < d.Words.Count; ++i){
            if (d.Words[i] == "peter") {
                d.Words[i] = "penelope";
            }
        }

        for (int i = 0; i < d.Characters.Count; ++i) {
            if (d.Characters[i] == 'p') {
                d.Characters[i] = 'P';
            }
        }

        Console.WriteLine(d.Text);
    }
}

}

如果我将上面标记的代码更改为:

if (count == wordCount) {//PROBLEM IS HERE
                        start = inWord;
                        length = i - inWord;
                        count++;
                        return true;
}

我得到一个 IndexOutOfRangeException,但我不知道为什么。

4

4 回答 4

3

在您的代码版本中,count仅在count == wordCount; 在微软版本中,无论是否满足条件都会递增。

于 2015-01-19T14:57:00.387 回答
3

您最初的假设是不正确的(这两个示例的工作原理完全相同)。在以下版本中,count无论是否等于 ,都会递增wordCount

if (count++ == wordCount)
{ 
    // Code omitted
}

在这个版本中,count只有当它等于wordCount

if (count == wordCount)
{ 
    // Other code omitted
    count++;
}

编辑

这导致您失败的原因是,当您搜索第二个单词(when wordCountis 1)时,变量count永远不会相等wordCount(因为它永远不会增加),因此该GetWord方法返回false,然后触发else您的子句get方法,它抛出一个IndexOutOfRangeException.

于 2015-01-19T15:04:40.013 回答
0
using System;

public class Test {

    public static void Main() {
        int a = 0;
        if(a++ == 0){
            Console.WriteLine(a);
        }
    }
}  

不完全一样:

public static void Main() {
    int a = 0;
    if(a == 0){
        a++;
        Console.WriteLine(a);
    }
}

在第二种情况下a++a == 0. 在第一种情况下a++,每次我们检查条件时都会执行。

于 2015-01-19T15:14:53.450 回答
-1

有你的错误:

public static void Main() {
        int a = 0;
        if(a == 0){
            a++;
            Console.WriteLine(a);
        }
}

它应该是这样的:

public static void Main() {
        int a = 0;
        if(a == 0){
            a++;
            Console.WriteLine(a);
        }
        else
            a++;
}

a 总是增加。这意味着,在您的代码示例中,计数只会在 count == wordCount 时增加(在这种情况下,该方法无论如何都会返回 true ......)。你基本上从不增加计数。

于 2015-01-19T15:12:20.570 回答