小编Vit*_*Din的帖子

如何重载空条件运算符“?”。

有一个简单的类:

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = null;
            int? t;
            t = test?.i;  // in this place the overloaded method "operator! =" is NOT called
            if (test != null) // in this place the overloaded method "operator! =" is called
            {
                t = test.i;
            }
        }
    }
    public class Test
    {
        public int i = 5;
        public override bool Equals(object obj)
        {
            return true;
        }
        public static bool operator ==(Test test1, Test …
Run Code Online (Sandbox Code Playgroud)

c#

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

标签 统计

c# ×1