小编Col*_*ler的帖子

如何覆盖`is`运算符

我们知道:

int? number = 10;
Console.WriteLine(number is int); // true
Run Code Online (Sandbox Code Playgroud)

但:

NotNull<string> text = "10"; // NotNull<> is my struct
Console.WriteLine(text is string); // false
Run Code Online (Sandbox Code Playgroud)

我想要text is string回归真实,我该怎么做?

--------------------------------编辑

这是我的NotNull:

public class NotNull<T> where T : class
{
    public T Value { get; }

    private NotNull(T value)
    {
        this.Value = value;
    }

    public static implicit operator T(NotNull<T> source)
    {
        return source.Value;
    }

    public static implicit explicit NotNull<T>(T value)
    {
        if (value == null) throw new ArgumentNullException(nameof(value));
        return new …
Run Code Online (Sandbox Code Playgroud)

c#

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

为什么Boolean不抛出StackOverflowException?

我在http://referencesource.microsoft.com/#mscorlib/system/boolean.cs上找到了布尔源代码:

public struct Boolean
{
    ...
    private bool m_value;
    ...
}
Run Code Online (Sandbox Code Playgroud)

为什么它不抛出StackOverflowException?

c#

7
推荐指数
1
解决办法
124
查看次数

标签 统计

c# ×2