小编use*_*065的帖子

为什么这样做?从IL执行方法没有实例

我正在浏览你在C#或.NET中看到的最奇怪的角落什么?,这段代码让我思考了一下:

public class Program
{
    delegate void HelloDelegate(Strange bar);

    [STAThread()]
    public static void Main(string[] args)
    {
        Strange bar = null;
        var hello = new DynamicMethod("ThisIsNull",
            typeof(void), new[] { typeof(Strange) },
         typeof(Strange).Module);
        ILGenerator il = hello.GetILGenerator(256);
        il.Emit(OpCodes.Ldarg_0);
        var foo = typeof(Strange).GetMethod("Foo");
        il.Emit(OpCodes.Call, foo);
        il.Emit(OpCodes.Ret);
        var print = (HelloDelegate)hello.CreateDelegate(typeof(HelloDelegate));
        print(bar);
        Console.ReadLine();
    }

    internal sealed class Strange
    {
       public void Foo()
       {
           Console.WriteLine(this == null);
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

我确实理解代码的作用,但我不明白为什么它有效.是不是喜欢这样做null.Foo()?它就好像Foo()是静态的,而是被调用:Strange.Foo();.

你能告诉我我错过了什么吗?

.net c# il

9
推荐指数
1
解决办法
193
查看次数

当内存只是正数时,我们为什么要使用int

我想知道为什么我们使用整数来获取内存中的地址,而内存地址只能是正数?为什么我们不使用无符号整数?

例如:
在C#中,为什么我们有IntPtr,为什么不用UIntPtr?
数组也有Int32.Max的限制.不过,为什么不签名,因为索引只能> = 0?
另外,在C++中,很多代码都将指针地址分配给int而不是uint,这背后有原因吗?这是不好的做法吗?

谢谢!

c# c++

5
推荐指数
0
解决办法
79
查看次数

为什么if(x)与if(window.x)不一样?

为什么这个:

if(x)
  alert('Available');
Run Code Online (Sandbox Code Playgroud)

得到: ReferenceError: x is not defined

虽然这有效:

if(window.x)
   alert('Available');
Run Code Online (Sandbox Code Playgroud)

不是说:

var x = "";
Run Code Online (Sandbox Code Playgroud)

相当于:

x = "";
Run Code Online (Sandbox Code Playgroud)

相当于:

window.x = "";
Run Code Online (Sandbox Code Playgroud)

只要一个函数外面,整个代码被一个包围着with(window)



为了更清楚:我知道VS成员变量全球性的区别,但我想知道为什么检索未声明的变量提供类似的ReferenceError x;同时window.x给出了不确定的?他们不应该给undefined?

javascript global-variables

4
推荐指数
1
解决办法
77
查看次数

标签 统计

c# ×2

.net ×1

c++ ×1

global-variables ×1

il ×1

javascript ×1