C#每个语句都可以表达吗? [英] C# Can every statement be an expression?

查看:86
本文介绍了C#每个语句都可以表达吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


每个语句都可以是C#中的表达式吗?例如,我知道方法调用语句可以用作表达式,因此我可以这样做:

Can every statement be an expression in C#? For example I know that a method invocation statement can be used as an expression and because of that I can do stuff like this:


for
(int i = 0; i< 10; Console.WriteLine(i ++));


但是情况如此每个陈述?

But is it the case with every statement?



但是由于某种原因,这个东西不适用于while循环

But this thing for some reason doesn't work with the while loop


<代码样式="保证金:0px;填充:1px 5px;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;字体大小:13像素;行高:继承; font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;垂直对齐:基线;背景色:#eff0f1;空格:预包装">而
(Console.WriteLine(1)> 0){}


这段代码给出了一个错误。也许你可以解释一下发生了什么?我有点困惑。

This code gives an error. Maybe you can explain what's happening? I got a bit confused.

推荐答案

Console.WriteLine方法的结果类型为void。所以你不能在while条件下使用它,因为你会将void与int值进行比较.Control.WriteLine(i ++)for循环工作因为i< 10是条件。对于循环有最后一个参数迭代器。
我可能是"一切"而且它不是强制性的(可能是空的)。迭代器参数可以是函数调用,lambda表达式等等......可能有许多用逗号分隔的表达式( ,)。关于定义什么是一次迭代
完成后的行为。

Console.WriteLine method has result type void. So you cannot use it in while condition because of you would compare void with int value. Console.WriteLine(i++) in for cycle works because of i<10 is condition. For cycle has last parameter iterator. I could be "everything" and it is not mandatory (could be empty). Iterator parameter could be function calling, lambda expression, etc... There could be many expressions separated by comma (,). It is about definition what is behavior after one iteration is finished.

你必须在循环另一个时写:

You must write while cycle another:

int i = 0;

int i = 0;

while(i ++< = 0)

while(i++ <= 0)

{

   Console.WriteLine(i);

    Console.WriteLine(i);

}

周期如何运作:

1)我是初始化为0.

1) i is initialize to 0.

2)条件i <10 - 这是真的=>转到正文

2) There is condition i < 10 - it is true => go to body

3)正文为空,转到迭代器

3) Body is empty, go to iterator

4)写入控制台值i(0)

4) Write to Console value of i (0)

5)将i设为i + 1

5) Set i as i+1

6)转到步骤2. 

6) Go to step 2. 

虽然循环没有迭代器部分,但它可以工作:

While cycle doesn't have iterator section so it works:

1)初始化必须在while循环之外。 

1) Initialize must be outside of while cycle. 

2)检查条件是否为真(与周期的第2步相同)

2) Check if condition is true (same as step 2 of for cycle)

3)如果为真,请转到正文

3) If true, go to body

4)转到第2步。 

4) Go to step 2. 

在循环中不是可以调用函数而不是循环的部分。 

In while cycle is not section where function could be called instead of for cycle. 

如果你想从文件中读取,你可以在示例中使用函数调用:

You could use function calling in example if you want to read from file:

string line = string.Empty;

string line=string.Empty;

while((line = textReader.ReadLine())!= null)

while((line = textReader.ReadLine())!=null)

{

}

但调用textReader.ReadLine的结果保存到行变量中,并与null进行比较。但是Console.WriteLine没有结果值。你能比较什么?

But result of calling textReader.ReadLine is saved into line variable and it is compare with null. But Console.WriteLine doesn't result value. What could you compare?


这篇关于C#每个语句都可以表达吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆