后缀增量为if,c#
代码示例:后缀增量为if,c#,c#,indexoutofboundsexception,postfix-operator,C#,Indexoutofboundsexception,Postfix Operator,代码示例: 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;
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);
}
}
这两个例子的工作原理完全相同(根据我对后缀的了解)。
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);
}
}
我得到一个IndexOutOfRangeException,但我不知道为什么。在您的代码版本中,
count
仅在count==wordCount
时递增;在Microsoft版本中,无论是否满足条件,它都会递增。在您的代码版本中,count
仅在count==wordCount
时才会递增;在Microsoft版本中,无论条件是否满足,都会递增。您最初的假设是不正确的(两个示例的工作原理完全相同)。在以下版本中,count
将递增,无论它是否等于wordCount
:
if (count++ == wordCount)
{
// Code omitted
}
在此版本中,count
仅在等于wordCount
if (count == wordCount)
{
// Other code omitted
count++;
}
编辑
这导致您失败的原因是,当您搜索第二个单词时(当
wordCount
是1
),变量count
永远不会等于wordCount
(因为它永远不会递增),因此GetWord
方法返回false
,然后触发get
方法中的else
子句,该子句抛出一个indexootfrangeexception
您最初的假设是不正确的(两个示例的工作原理完全相同)。在以下版本中,count
将递增,无论它是否等于wordCount
:
if (count++ == wordCount)
{
// Code omitted
}
在此版本中,count
仅在等于wordCount
if (count == wordCount)
{
// Other code omitted
count++;
}
编辑
这导致您失败的原因是,当您搜索第二个单词时(当wordCount
是1
),变量count
永远不会等于wordCount
(因为它永远不会递增),因此GetWord
方法返回false
,然后触发get
方法中的else
子句,该子句抛出索引自动异常
这是您的错误:
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时,count才会增加(在这种情况下,该方法将返回true…)。您基本上从不增加计数。这是您的错误:
public static void Main() {
int a = 0;
if(a == 0){
a++;
Console.WriteLine(a);
}
}
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);
}
else
a++;
}
a一直在增加。这意味着,在代码示例中,只有当count==wordCount时,count才会增加(在这种情况下,该方法将返回true…)。你基本上从不增加计数
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++
在每次检查条件时执行
不完全相同于:
public static void Main() {
int a = 0;
if(a == 0){
a++;
Console.WriteLine(a);
}
}
在第二种情况下,
a++
仅在a==0
时执行。在第一种情况下,a++
在我们每次检查条件时都会执行。那么,抛出异常的确切位置是哪里?此时变量(在[]
中)的值是多少?(使用断点,使用调试器)尝试将示例放入循环中,看看区别是什么。标记为“问题”的行不会引发indexoutfrange
异常。哪一行抛出了它?它是WordCollection索引属性的get抛出的异常。那么,异常到底在哪里抛出?此时变量(在[]
中)的值是多少?(使用断点,使用调试器)尝试将示例放入循环中,看看区别是什么。标记为“问题”的行不会引发indexoutfrange
异常。哪一行抛出了它?这是WordCollection索引属性的get抛出的异常。您没有解释为什么这会导致异常;我假设,因为这是工作代码和损坏代码之间唯一的功能区别,它最终会导致OP看到的索引自动失效异常
。但你是对的,我没有花时间跟踪到异常,这会得到更好的答案。如果你已经完成了这项工作,一定要添加一个更好的答案并赢得一些声誉。(编辑:我看到你添加了一个答案。)你没有解释为什么这会导致例外;我假设,因为这是工作代码和损坏代码之间唯一的功能区别,它最终会导致OP看到的索引自动失效异常
。但你是对的,我没有花时间跟踪到异常,这会得到更好的答案。如果你已经完成了这项工作,那么一定要添加一个更好的答案并赢得一些声誉。(编辑:我看到你添加了一个答案。)@Selman22或任何被否决的人,我添加了导致异常的原因解释(鉴于我最初对假设错误的解释,我认为这是很明显的,但可能不是?。@Selman22或任何被否决的人,我添加了导致异常的原因解释(考虑到我最初对假设错误的解释,我认为这是很明显的,但可能不是?)。你的回答是正确的,我不知道你为什么被否决。我选择了另一个答案,因为它也解释了它抛出异常的原因。非常感谢。在我能够编辑我的答案之前,我得到了一次否决票(代码格式已经消失:()你的答案是正确的,我不知道你为什么被否决。我选择了另一个