小编use*_*024的帖子

在检查null时,泛型函数是否隐式地将值类型转换为对象?

例如,以下代码演示了我的思路:

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# null reference value-type

2
推荐指数
2
解决办法
502
查看次数

使用UnSafe代码使指针移动到自定义类的下一个元素

是否可以在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类型等来做,但是用户定义的结构和类呢?

c# pointers unsafe

0
推荐指数
1
解决办法
676
查看次数

标签 统计

c# ×2

null ×1

pointers ×1

reference ×1

unsafe ×1

value-type ×1