例如,以下代码演示了我的思路:
class Program
{
static void Main(string[] args)
{
int i = 0;
IsNull(i); // Works fine
string s = null;
IsNull(s); // Blows up
}
static void IsNull<T>(T obj)
{
if (obj == null)
throw new NullReferenceException();
}
}
Run Code Online (Sandbox Code Playgroud)
还有以下代码:
int i = 0;
bool b = i == null; // Always false
Run Code Online (Sandbox Code Playgroud)
是否存在隐式对象?这样:
int i = 0;
bool b = (object)i == null;
Run Code Online (Sandbox Code Playgroud) 是否可以在C#中执行以下操作?
unsafe string GetName()
{
Foo[] foo = new Foo[2]; // Create an array of Foo and add two Foo elements
foo[0] = new Foo { Name = "Bob" };
foo[1] = new Foo { Name = "Jane" };
Foo *ptr = &foo; // Get address of the first element in the array
ptr++; // Move to the next element in the array
return *ptr.Name; // Expect Name to be "Jane"
}
Run Code Online (Sandbox Code Playgroud)
我正在玩自定义数据结构,我希望能够做到这一点.
我知道你可以用int类型等来做,但是用户定义的结构和类呢?