Пример #1
0
    public object whoAreYou(IMyInterface1 imi)
    {
        string s = "jocke";

        Console.WriteLine(imi.ToString());
        return(imi.GetType());
    }
Пример #2
0
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();

            IMyInterface1 myClass2 = myClass;
            var           name1    = myClass2.Name();

            Console.WriteLine(name1);

            IMyInterface2 myClass3 = myClass;
            var           name2    = myClass3.Name();

            Console.WriteLine(name2);
        }
Пример #3
0
 public bool Temp(IMyInterface1 imi)
 {
     Person p = imi as Person;
     Person p2 = p;
     Console.WriteLine(p.Equals(p2));
     Console.WriteLine(this);
     return this.Equals(p2);
 }
Пример #4
0
 public object whoAreYou(IMyInterface1 imi)
 {
     Console.WriteLine(imi);
     Person p = imi as Person;
     return p.Name.GetType();
 }
Пример #5
0
 public object whoAreYou(IMyInterface1 imi)
 {
     Console.WriteLine(imi);
     return imi.GetType();
 }
Пример #6
0
 // 建構函式 5
 public YourClass(IMyInterface1 myInterface1, string myString, int myInt)
 {
     Console.WriteLine("YourClass 建構式5(IMyInterface1, string, int) 被呼叫");
 }
Пример #7
0
 // 建構函式 2
 public YourClass(IMyInterface1 myInterface1, IMyInterface2 myInterface2)
 {
     Console.WriteLine("YourClass 建構式2(IMyInterface1, IMyInterface2) 被呼叫");
 }
Пример #8
0
        static void Main(string[] args)
        {   //difference between overridin and hiding?

            //implicit vs explicit implementation of interface memebers
            MyClass7 obj7 = new MyClass7();
            obj7.InterfaceKill(); //implicitely defined but cant access function implementMeh
            obj7.whatever();//method declared and define in class
            IMyInterface1 myInt = obj7;
            myInt.InterfaceKill();
            myInt.explicitelyImplementMehPls(); //Now we can call it!!!
            //myInt.whatever(); interface code cant access this

            MyBaseClass zuluHide = new derievedClass1(); //Hiding base class implementation
            MyBaseClass zuluOverride = new derievedClass2(); //Overriding

            Console.WriteLine($"Hiding: {zuluHide.add(2, 5)}"); //Ran base class method, not polymorphic
            Console.WriteLine($"Hiding: {zuluOverride.add(2, 5)}"); //called polymorphic overriden method

            classA A = new classA();
            Console.WriteLine($"{A.State}");
            classA.classB B = new classA.classB();
            B.SetPrivateState(A, 999);  //This brings up another point however, how were we able to change the
            //object if it was pass by value and not pass by reference?
            Console.WriteLine($"{A.State}");
            ReadKey();
            //Answer:
            /*
             Since classA is a class (which are reference objects), 
             you can change the contents inside A without passing it as a ref. 
             However, if you pass A as a ref, SetPrivateState can change what the original 
             A refers to. i.e. make it point to a different object.
            
            A reference type is type which has as its value a reference to the appropriate data
            rathar than the data itself
            StringBuilder sb = new StringBuilder();
            sb, create a new string builder object, and assign sb as a reference to the object
            class, interface,delegate, array types are all reference types
            structs and enums are value types
            While reference types have layer of indirection between variable and real data,
            value type dont. variables of value type directly contain data.
            4 types of parameters in functions, default which is value, 
            reference ref, output out, and parameter arrays(params)
            You can use any of them with both value and reference types!
            By default parameter value type, new storage location created
            for the variable in the function member declaration and it starts off
            with the value that you specify in the function member invocation.
  ////////////////////////////////////
          void Foo (StringBuilder x){
                x = null;
            }

            StringBuilder y = new StringBuilder();
            y.Append("hello");
            Foo(y);
            Console.WriteLine(y == null); //This is false because x is a copy of what y refers  but now x refers to null
            //if x was a ref parameter, then this would be true because they 
            dont pass the values of variables, they use the variables themselves
            //The same storage locaiton is used rather than creating a new storage location

/////////////////////////////////////////////////
            void Foo (StringBuilder x){
                x.Append(" world");
            }

            StringBuilder y = new StringBuilder();
            y.Append("hello");
            Foo(y);
            //When you pass y which is a class so reference type, do not think of this as
            object being passed by reference, but object reference passed by value
            Console.WriteLine(y); //returns "hello world", 
            //if y was value type, y would remain "hello" unless it was a ref parameter,
           //then even if y was value type it would be "hello world"

            Output parameters are very similar to reference parameters. The only differences are:
            The parameter must be assigned a value before the function member completes normally.

            /////////////////////////////////////////////////
 
             */

            //System.Object members
            //all objects have a ToString() method which is a method defined in System.Object and which
            //derieved classes can override to output something else. By default, ToString() method 
            //returns class name of the object as a string qualified by any relevant namespaces. 
            //Object Methods:
            //Object() => Constructor
            //~Object() => Destructor
            //static virtual bool Equals(object,object) => checks whther the object paramter refers to the same object
            //override for real check of states
            //static bool ReferenceEquals(object,object) ==> checks whther they are references to the same instnace
            //virtual string ToString() overide pls
            //object MemberwiseClone() ==> protected method can only be used used from within the class or derieved classes
            //member copies references which lead to references to samee objects
            //System.Type GetType() //returns type in form of System.Type object
            //virtual int GetHashCode() //Used as a hash function for objects where this is required. A hash function
            //returns a value identifying the object state in some compressed form.
            Stopwatch watch = new Stopwatch();
            watch.Start();

            //Using typoof and getType =>
            object fun = new MyClass3();
            if (fun.GetType() == typeof(MyClass3))
            {
                Console.WriteLine("They are the same");
            }

            MyClass myObj = new MyClass();
            for (int i = -1; i <= 0; i++)
            {
                try
                {
                    myObj.MyIntProperty = i;
                }
                catch (Exception e)
                {

                    WriteLine($"Exception {e.GetType().FullName} thrown.");
                    WriteLine($"Message: \n\"{e.Message}\"");
                }

            }


            MyClass3 obj3 = new MyClass3();
            obj3.MyIntProp = 5;
            Console.WriteLine($"Automatic Properties are kwel {obj3.MyIntProp}");

            //Classes are reference types and structs are value types
            MyyClass objectA = new MyyClass();
            MyyClass objectB = objectA;//When you assign an object to var, you are actually assigning var with a pointer to which the object refers
            objectA.val = 10;
            objectB.val = 20;
            MyyStruct structA = new MyyStruct();
            MyyStruct structB = structA;//Copying all the information from one struct to another
            structA.val = 10;
            structB.val = 20;

            WriteLine($"objectA val: {objectA.val}");
            WriteLine($"objectB val: {objectB.val}");
            WriteLine($"structA val: {structA.val}");
            WriteLine($"structB val: {structB.val}");

            //TO implement deep copying use interface ICloneable and define function Clone() which returns System.Object


            orientation direction = orientation.North;

            bool success = false;
            do
            {
                try
                {
                    WriteLine("What direction do you want to go (1,2,3,4)");
                    string input = ReadLine();
                    byte number = Convert.ToByte(input);
                    if (number < 0 || number > 4)
                    {
                        throw new System.Exception();
                    }
                    direction = checked((orientation)number);
                    success = true;
                }
                catch (System.InvalidCastException e)
                {
                    WriteLine("Please try again");
                }
                catch (System.FormatException e)
                {
                    WriteLine("Please try again");
                }
                catch
                {
                    WriteLine("Please Try AGGAIN");
                }
            } while (success == false);

            WriteLine("Direction is {0}", direction);




            foreach (string eType in eTypes)
            {
                try
                {
                    WriteLine("Main() try block reached.");        // Line 21
                    WriteLine($"ThrowException(\"{eType}\") called.");
                    ThrowException(eType);
                    WriteLine("Main() try block continues.");      // Line 24
                }
                catch (System.IndexOutOfRangeException e) when (eType == "filter")            // Line 26
                {
                    BackgroundColor = ConsoleColor.Red;
                    WriteLine($"Main() FILTERED System.IndexOutOfRangeException catch block reached. Message:\n\"{e.Message}\"");
                    ResetColor();
                }
                catch (System.IndexOutOfRangeException e)              // Line 32
                {
                    WriteLine($"Main() System.IndexOutOfRangeException catch block reached. Message:\n\"{e.Message}\"");
                }
                catch                                                    // Line 36
                {
                    WriteLine("Main() general catch block reached.");
                }
                finally
                {
                    WriteLine("Main() finally block reached.");
                }
                WriteLine();
            }

            ReadKey();
            watch.Stop();
            WriteLine($"You spent this time on program: {watch.Elapsed}");

        }