Beispiel #1
0
        /// <summary>
        /// Tests the <see cref="IEquatable{T}.Equals(T)"/> implementation
        /// when dealing with nulls.
        /// </summary>
        /// <param name="obj">An object not <b>null</b>.</param>
        /// <remarks>
        /// <para>
        /// Objects being <b>null</b> are only equal to other <b>null</b>
        /// objects.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="obj"/> is <b>null</b>.
        /// </exception>
        public static void EqualsWithNulls <T>(T obj)
            where T : class,
        IEquatable <T>,
        IComparable <T>,
        IComparable
        {
            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            Assert.IsFalse(obj.Equals((T)null));

            UnknownClass unknownClass = null;

            Assert.IsFalse(obj.Equals(unknownClass));

            Assert.IsTrue((T)null == (T)null);

            Assert.IsFalse((T)null == obj);

            Assert.IsFalse(obj == (T)null);

            Assert.IsFalse((T)null != (T)null);

            Assert.IsTrue((T)null != obj);

            Assert.IsTrue(obj != (T)null);
        }
Beispiel #2
0
        public void MethodUnknownClass()
        {
            //IL_0001: Unknown result type (might be due to invalid IL or missing references)
            //IL_0007: Expected O, but got Unknown
            UnknownClass val             = new UnknownClass();
            int?         unknownProperty = val.UnknownProperty;
            int?         num             = unknownProperty.GetValueOrDefault();

            val.UnknownProperty = num;
            int?          num2 = num;
            List <object> list = new List <object> {
                val[unknownProperty.Value] ?? "",
                val.NotProperty,
                val.get_NotPropertyWithGeneric <string>(42),
                val[42],
                val.get_NotPropertyWithParameterAndGeneric <object>(int.MinValue),
                val.get_PropertyCalledGet,
                val.set_HasReturnType(),
                val.set_HasReturnType("")
            };

            val.get_NoReturnType();
            val.set_NoValue();
            val.OnEvent += Instance_OnEvent;
            val.OnEvent -= Instance_OnEvent;
            string text = val[(long?)null];

            val[(long?)long.MaxValue] = text;
            IntPtr intPtr = val[UIntPtr.Zero, "Hello"];

            val[(UIntPtr)32uL, "World"] = intPtr;
        }
Beispiel #3
0
        private static void DemoSwitchExpressionWithPatternMatching()
        {
            Console.WriteLine(GetJustifiedText("DemoSwitchExpressionWithPatternMatching starts"));
            object obj1 = 1000;
            object obj2 = new SuperHero {
                Name = "Bruce Wayne"
            };
            object obj3 = new SuperHero {
                Name = "Steve Rogers"
            };
            object obj4    = null;
            object obj5    = new UnknownClass();
            var    result1 = obj1 switch
            {
                // Match on if it is of type String and also starts with "abc
                String s when s.StartsWith("abc") => "Its a string that starts with abc",

                // Match if it is of type String
                String s => "Its a string",

                // This is called Property based pattern
                // Match if it is of type SuperHero and also its Name property should have value "Bruce Wayne" and CabFly property true
                SuperHero
                {
                    Name : "Bruce Wayne", CanFly : true
                }