为什么在CIL中,编译器在使用数组时将foreach循环转换为for循环,而在使用List<T>时则使用迭代器模式?
如果System.Array和System.Collections.Generic.List<T>都实现了IEnumerable,那么它们不应该都在幕后使用迭代器模式吗?
下面是一个示例:
控制台App1:
C#:
class Program
{
static void Main(string[] args)
{
var enumerable = new List<string> { "a",
值类型可以存储在线程的堆栈中,IL在执行堆栈中运行(抽象概念)。
int y=0;
int a=0;
int b=0;
int x = y + (a - b);
IL_0001: ldc.i4.0
IL_0002: stloc.0 // y
IL_0003: ldc.i4.0
IL_0004: stloc.1 // a
IL_0005: ldc.i4.0
IL_0006: stloc.2 // b
IL_0007: ldloc.0 // y
IL_0008: ldloc.1 // a
IL_0009: ldl
在C#规范的8.8.4中,它提供了以下示例:
表格的前瞻陈述
foreach (V v in x) embedded-statement
然后扩展为:
{
E e = ((C)(x)).GetEnumerator();
try {
V v;
while (e.MoveNext()) {
v = (V)(T)e.Current;
embedded-statement
}
}
finally {
… // Dispose e
}
}
我正在使用DevIl库,并尝试使用它将纹理加载到OpenGL以应用于精灵。代码直接来自C#严肃游戏设计的游戏编程一书(如果有帮助的话)。我遇到的问题是Il.ilLoadImage调用。即使我传递null,它也不会抛出image not found错误,当窗体弹出时,sprite只会显示深灰色(而不是白色)。
public void LoadTexture(string textureId, string path)
{
int devilId = 0;
Il.ilGenImages(1, out devilId);
Il.ilBindIm
我正在通过C#游戏编程为严肃的游戏创作工作,我似乎遇到了障碍。
以下代码似乎不起作用。ie openGLId返回0。
纹理似乎是从磁盘加载的,ok。但是没有Id附加到它。
public void LoadTexture(string textureId, string path)
{
int devilId = 0;
Il.ilGenImages(1, out devilId);
Il.ilBindImage(devilId); // set as the active texture
代码如下:
public void LoadTexture(string textureId, string path)
{
int devilId = 0;
Il.ilGenImages(1, out devilId);
Il.ilBindImage(devilId); // set as the active texture
if (!Il.ilLoadImage(path))
{
System.Diagnostics.Debug.Assert(false, "Cou
给定以下C#代码,其中以两种不同的方式调用Dispose方法:
class Disposable : IDisposable
{
public void Dispose()
{
}
}
class Program
{
static void Main(string[] args)
{
using (var disposable1 = new Disposable())
{
Console.WriteLine("using");
}
var disp
当我打开像ILSpy或dotPeek这样的工具时,我可以选择查看反编译的C#或IL。
当您查看反编译源时,反编译器是否将IL反向工程成反编译的C#?
如果是这样的话,那么反编译者将如何推断像下面这样的IL?(请注意,这是第一语言构造的一个微不足道的例子,它很难从IL中推断出来,而不是反编译器的实际输出):
IL_0000: nop // Do nothing (No operation)
IL_0001: ldstr "C" // Push a string object for the literal string
IL
这是我第一次在C#和ASP.NET上工作,我正在阅读c# 2010中开始的ASP.NET 4,但是我从来没有使用过面向对象的编程。(我是一个网络管理员,所以我知道基本编程)。
尽管如此,我还是无法完成一个简单的程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2 {
class Program {
static void Main(string[] args) {
我有一个本地化的NSString,类似于“你赢了,那是查克·诺里斯。”这本书的法语翻译是“Gagne.il s‘’agissait bien de Chuck Norris”。但当名字以元音开头时,应该是“Gagné.il s‘’agissait bien d‘’Alfred”。
NSString是否提供了对这些情况的支持,或者我是否需要手动转换每种可能性?
我继承了一些代码,其中有很多我想要删除的警告。许多都是Property '<propertyname>' doesn't return a value on all code paths格式的。我知道它为什么这么说,我只是想确定修复这个问题的正确的0影响方式。
原来的代码是:
Public ReadOnly Property LevelName() As String Implements IMember.LevelName
Get
End Get
End Property
我的第一个想法是,我可以在其中放置一个Return Nothing
对于以下代码片段:
struct Test
{
public override string ToString()
{
return "";
}
}
public class Program
{
public static void Main()
{
Test a = new Test();
a.ToString();
Int32 b = 5;
b.ToString();
}
}
编译器发出以下IL:
.locals init ([0] valu