static void Main(string[] args) { // Testing interfaces, abstract classes, delegate-event-handler. SwordsMan hero_sm = new SwordsMan("Maxim"); Boss monst_boss = new Boss("MegaBoss"); Console.WriteLine(hero_sm); Console.WriteLine(monst_boss); // Testing raising an exception. for (int i = 0; i < 10; i++) { try { monst_boss.dealDamage(hero: hero_sm); } catch (Exception ex) { Console.WriteLine("Message: {0}", ex.Message); Console.WriteLine("Target method: {0}", ex.TargetSite); Console.WriteLine("InnerException: {0}", ex.InnerException); } } // End of the Lab 2 test case. Logger.printMsgGame("\nEnd of the Lab 2."); Console.ReadLine(); }
static void Test6_BinarySerialization() { SwordsMan h = new SwordsMan("Maxim"); Console.WriteLine(h); // Serialize to Binary. Console.WriteLine("Start serialization"); FileStream fs = new FileStream("swordsman.dat", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, h); fs.Close(); // Deserialize from Binary. Console.WriteLine("Start deserialization"); FileStream fs2 = new FileStream("swordsman.dat", FileMode.Open); BinaryFormatter bf2 = new BinaryFormatter(); SwordsMan h2 = (SwordsMan)bf2.Deserialize(fs2); fs2.Close(); // Print the result. Console.WriteLine("Before"); Console.WriteLine(h); Console.WriteLine("After"); Console.WriteLine(h2); }
static void Main(string[] args) { Logger.printMsgGame("Start of the Lab 4."); // Creating an object. Console.WriteLine("Start of the test case: {0}", GC.GetTotalMemory(true)); SwordsMan s = new SwordsMan("The man"); for (int i = 0; i < 0xffffff; i++) { s.dump.Add((decimal)0xffffffffffffffff); } Console.WriteLine("After creating an object: {0}", GC.GetTotalMemory(true)); // Weak reference test. WeakReference wr = new WeakReference(s); s = null; GC.Collect(); s = (SwordsMan)wr.Target; if (s != null) { Console.WriteLine("Swordsman is not collected!"); } else { Console.WriteLine("Object has been collected."); } // Dispose test. // Dispose is a command, that flags an object, that a program does not need it anymore. Console.WriteLine("Disposing an object."); Console.WriteLine("Before: {0}", GC.GetTotalMemory(true)); s.Dispose(); if (s != null) { Console.WriteLine("Swordsman is not collected!"); } else { Console.WriteLine("Object has been collected."); } Console.WriteLine(GC.GetTotalMemory(true)); // A destructor test. SwordsMan tmp = new SwordsMan("tmp"); tmp = null; GC.Collect(); // End of the Lab 4 test case. Logger.printMsgGame("End of the Lab 4."); Console.ReadLine(); }
static void Test2_Lambdas() { SwordsMan s = new SwordsMan("Maxim"); Boss b = new Boss("MegaBoss"); s.HeroHit += () => { Console.WriteLine("psst, someone will attack this hero soon!"); }; b.MonsterHit += () => { Console.WriteLine("psst, someone will attack this monster soon!"); }; Console.WriteLine(""); b.dealDamage(hero: s); Console.WriteLine(""); s.dealDamage(monster: b); Console.WriteLine(Environment.NewLine); }
static void Test3_FuncAction() { SwordsMan s = new SwordsMan("Maxim"); // Print position before and after moving a hero on the map. Console.WriteLine(s.getSpecificPosition()); s.moveHero(20, 20); Console.WriteLine(s.getSpecificPosition()); Console.WriteLine(""); // Print position before and after moving a hero on the map. Console.WriteLine(s.getSpecificPosition()); s.moveHero(200, 200); Console.WriteLine(s.getSpecificPosition()); }