C#递增运算符(++)的问题:为什么我会得到错误的输出? [英] C# Increment operator (++) question:Why am i getting wrong output?
本文介绍了C#递增运算符(++)的问题:为什么我会得到错误的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的C#控制台应用程序,但我得到的输出错误,为什么
使用系统?;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间ConsoleApplication11
{
类节目
{
静态无效的主要(字串[] args)
{
INT I = 100;
为(INT N = 0; N< 100; N ++)
{
I = I ++;
}
Console.WriteLine(ⅰ);
}
}
}
解决方案
我++
是返回 I
表达式,然后递增它。
因此, I = I +
将评估 I
,增量 I
,然后分配 I
到原始的值,然后才递增。
您需要使用 ++我
,这将返回递增的值。
I have a simple c# console application but i am getting wrong output why?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int i = 100;
for (int n = 0; n < 100; n++)
{
i = i++;
}
Console.WriteLine(i);
}
}
}
解决方案
i++
is an expression which returns i
, then increments it.
Therefore, i = i++
will evaluate i
, increment i
, then assign i
to the original value, before it was incremented.
You need to use ++i
, which will return the incremented value.
这篇关于C#递增运算符(++)的问题:为什么我会得到错误的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文