你如何在C#中做"内联函数"?我认为我不理解这个概念.他们喜欢匿名方法吗?像lambda函数?
注意:答案几乎完全处理内联函数的能力,即"用被调用者的主体替换函数调用站点的手动或编译器优化".如果您对匿名(也称为lambda)函数感兴趣,请参阅@ jalf的答案或者每个人都在说什么'Lambda'?.
我有两个这样嵌套的for循环:
for(...) {
for(...) {
}
}
Run Code Online (Sandbox Code Playgroud)
我知道有一个break声明.但我很困惑,如果它打破了两个循环或只是它被调用的那个?一旦我发现重复迭代次数没有意义,我需要打破这两个.
可能重复:
循环功能结果时foreach如何工作?
如果我具有以下功能 - 将在foreach循环中为每次迭代调用ReturnParts(),还是只调用一次?
private void PrintParts()
{
foreach(string part in ReturnParts())
{
// Do Something or other.
}
}
private string[] ReturnParts()
{
// Build up and return an array.
}
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法来编写这段代码而不使用goto?这看起来很尴尬,但我想不出更好的方法.我需要能够执行一次重试尝试,但我不想复制任何代码.
public void Write(string body)
{
bool retry = false;
RetryPoint:
try
{
m_Outputfile.Write(body);
m_Outputfile.Flush();
}
catch (Exception)
{
if( retry )
throw;
// try to re-open the file...
m_Outputfile = new StreamWriter(m_Filepath, true);
retry = true;
goto RetryPoint;
}
}
Run Code Online (Sandbox Code Playgroud) 以下2个C#片段之间的执行存在差异吗?
do
{
Console.WriteLine(x.ToString());
++x;
}
while (x < 7);
Run Code Online (Sandbox Code Playgroud)
和
label:
{
Console.WriteLine(x.ToString());
++x;
}
if (x < 7) goto label;
Run Code Online (Sandbox Code Playgroud)
我想弄清楚为什么这么糟糕.谢谢.
编辑:如果我添加括号,片段非常相似.
EDIT2:在Visual Studio中,我点击了Go to Disassembly,我得到以下代码:
do
{
00000037 nop
Console.WriteLine(x.ToString());
00000038 lea ecx,[ebp-40h]
0000003b call 63129C98
00000040 mov dword ptr [ebp-48h],eax
00000043 mov ecx,dword ptr [ebp-48h]
00000046 call 63148168
0000004b nop
++x;
0000004c inc dword ptr [ebp-40h]
}
0000004f nop
while (x < 7);
00000050 cmp dword ptr [ebp-40h],7
00000054 setl al
00000057 movzx eax,al
0000005a mov …Run Code Online (Sandbox Code Playgroud) 我一直在使用反射器来反编译几个简单的c#应用程序,但我注意到虽然代码被反编译,但我仍然看不到它们写在VS上的东西.我认为这就是编译器用机器代码替换人类指令的方式.但是我想我会尝试一下并在这里问一下.也许有一个反编译器可以反编译并显示与原始代码几乎完全相同的编码.
c# ×6
goto ×3
.net ×2
loops ×2
c ×1
coding-style ×1
decompiling ×1
do-while ×1
inline ×1
objective-c ×1
optimization ×1
syntax ×1