// Another unary operator overloaded, ++;
        public static UnaryBinaryOperatorOverloading operator ++(UnaryBinaryOperatorOverloading op1)
        {
            UnaryBinaryOperatorOverloading result = new UnaryBinaryOperatorOverloading();

            result.x = ++op1.x; // These are integer additions
            result.y = ++op1.y; // and the + retains its original
            result.z = ++op1.z; // meaning relative to them.
            return(result);
        }
        // Overload binary, but with an integer. That way, we can add an integer to the entire object.
        public static UnaryBinaryOperatorOverloading operator +(UnaryBinaryOperatorOverloading op1, int shift)
        {
            UnaryBinaryOperatorOverloading result = new UnaryBinaryOperatorOverloading();

            result.x = op1.x - shift; // These are integer additions
            result.y = op1.y - shift; // and the + retains its original
            result.z = op1.z - shift; // meaning relative to them.
            return(result);
        }
        // Interestingly enough, you can override | and & operators as well. Note however to override && and ||, we must 4 things, which
        public static UnaryBinaryOperatorOverloading operator |(UnaryBinaryOperatorOverloading op1, UnaryBinaryOperatorOverloading op2)
        {
            UnaryBinaryOperatorOverloading result = new UnaryBinaryOperatorOverloading();

            result.x = op1.x + op2.x;
            result.y = op1.y + op2.y;
            result.z = op1.z + op2.z;

            return(result);
        }
        // Overload unary.
        public static UnaryBinaryOperatorOverloading operator -(UnaryBinaryOperatorOverloading op1)
        {
            UnaryBinaryOperatorOverloading result = new UnaryBinaryOperatorOverloading();

            /* Note that the result is a new object, meaning we cannot write op1.x = -op1.x, which is quite important. */
            result.x = -op1.x; // These are integer additions
            result.y = -op1.y; // and the + retains its original
            result.z = -op1.z; // meaning relative to them.
            return(result);
        }
        // Overload binary +.
        // Pay attention to the fact the method is static. And that it takes two objects op1 and op2. Had it taken one, it would have been a uniray
        // operator, which is useful for defining -op1, as shown below.
        public static UnaryBinaryOperatorOverloading operator +(UnaryBinaryOperatorOverloading op1, UnaryBinaryOperatorOverloading op2)
        {
            UnaryBinaryOperatorOverloading result = new UnaryBinaryOperatorOverloading();

            /* This adds together the coordinates of the two points
             * and returns the result. */
            result.x = op1.x + op2.x; // These are integer additions
            result.y = op1.y + op2.y; // and the + retains its original
            result.z = op1.z + op2.z; // meaning relative to them.
            return(result);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            RefAndOur access = new RefAndOur();

            int i = 5;

            access.Sqr(ref i);  // Very good that you have to actually give the ref here. i has to be initialized, or else we get an error.
            Console.WriteLine(i);

            double frac;

            i = access.GetParts(12.25, out frac);   // This is interesting and potentially very useful, you must declare a variable being out.
                                                    // Contrary to the above, frac does not need to be initialized.
            Console.WriteLine(i + " and " + frac);

            // See for yourself!
            RefToRef first  = new RefToRef(1);
            RefToRef second = new RefToRef(2);
            RefToRef third;

            first.SwapCreate(ref first, ref second, out third);
            Console.WriteLine(first.member + " " + second.member + " " + third.member);


            // Variable input
            VariableFunctionIput variable1 = new VariableFunctionIput('a', 1, 2, 3);
            VariableFunctionIput variable2 = new VariableFunctionIput('a');
            VariableFunctionIput variable3 = new VariableFunctionIput('a', 1);


            // Conversion due to overload is important
            CoversionInOverloading coversionInOverloading = new CoversionInOverloading();


            // Constructor overloading.
            ConstructorOverloading constructorOverloading  = new ConstructorOverloading();
            ConstructorOverloading constructorOverloading1 = new ConstructorOverloading(1, 1);
            ConstructorOverloading constructorOverloading2 = new ConstructorOverloading(constructorOverloading1);
            ConstructorOverloading constructorOverloading3 = new ConstructorOverloading(1);

            // How static classes work.
            StaticClass.method1();  // Can be accessed from a static class.
            ClassWithStaticMembers.method1();

            // Instantiating a class with a static constructor
            StaticConstructor ob  = new StaticConstructor(); // The good thing is that the static constructor cannot be called separately from the dynamic one.
            StaticConstructor ob1 = new StaticConstructor(); // The static constructor is also only called once, which is very good!

            Console.WriteLine("Cons.alpha: " + StaticConstructor.alpha);
            Console.WriteLine("ob.beta: " + ob.beta);


            // Instantiating classes with overloaded operator.
            UnaryBinaryOperatorOverloading unaryBinary = new UnaryBinaryOperatorOverloading(1, 2, 3);

            unaryBinary = -unaryBinary;    // Overloaded operator.
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            unaryBinary += 5;   // Overloaded operator.
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            unaryBinary = unaryBinary + unaryBinary; // Overloaded operator.
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            unaryBinary++; // Overloaded operator.
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            if (unaryBinary)
            {
                Console.WriteLine("This object is non-zero, hence it is a true!");
            }

            unaryBinary = unaryBinary | unaryBinary;    // Overloaded and!
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);


            // Type conversion.
            // This is explicit type conversion, where we explicitly perform casting to convert types.
            ExplicitTypeConversion typeConversion = new ExplicitTypeConversion(1, 2, 3, 4);

            unaryBinary = (UnaryBinaryOperatorOverloading)typeConversion;
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            // This is implicit type conversion, where type conversion happens without casting (which I don't think is a good idea).
            ImplicitTypeConversion implicitTypeConversion = new ImplicitTypeConversion(5, 6, 7, 8);

            unaryBinary = implicitTypeConversion;
            Console.WriteLine(unaryBinary.x + " " + unaryBinary.y + " " + unaryBinary.z);

            Console.ReadLine();
        }