我正在浏览你在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();.
你能告诉我我错过了什么吗?
我想知道为什么我们使用整数来获取内存中的地址,而内存地址只能是正数?为什么我们不使用无符号整数?
例如:
在C#中,为什么我们有IntPtr,为什么不用UIntPtr?
数组也有Int32.Max的限制.不过,为什么不签名,因为索引只能> = 0?
另外,在C++中,很多代码都将指针地址分配给int而不是uint,这背后有原因吗?这是不好的做法吗?
谢谢!
为什么这个:
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?