예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("========TEST1========");

            OperateTransformer city = new OperateTransformer();

            for (int i = 0; i < 1000; i++)
            {
                city.gen(i);
            }
            Console.WriteLine("\nAfter loop:\n");

            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            city = null;

            for (int i = 0; i < 1000; i++)
            {
                city = new OperateTransformer();
                // city.Dispose(); // GC for memory
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            Console.WriteLine("\nHighest gen: {0}", GC.MaxGeneration);

            Transformer tr = null;

            for (int i = 0; i < 30000; i++)
            {
                tr = new Transformer();
            }

            Console.WriteLine("GC: 0");
            Console.WriteLine("Generation: {0}", GC.GetGeneration(tr));
            Console.WriteLine("Total Memory: {0}\n", GC.GetTotalMemory(false));
            GC.Collect(0);

            Console.WriteLine("GC: 1");
            Console.WriteLine("Generation: {0}", GC.GetGeneration(tr));
            Console.WriteLine("Total Memory: {0}\n", GC.GetTotalMemory(false));
            GC.Collect(1);

            Console.WriteLine("GC: 2");
            Console.WriteLine("Generation: {0}", GC.GetGeneration(tr));
            Console.WriteLine("Total Memory: {0}\n", GC.GetTotalMemory(false));
            GC.Collect(2);

            GC.WaitForPendingFinalizers();

            Console.WriteLine("Total Memory in GC: {0}\n\n", GC.GetTotalMemory(false));


            _weak = Alloc();

            // See if weak reference is alive.
            if (_weak.IsAlive)
            {
                Console.WriteLine("Weak reference is alive");
            }

            Console.WriteLine("Generation for weak: {0}", GC.GetGeneration(_weak));
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            GC.Collect(0);
            GC.WaitForPendingFinalizers();

            if (_weak.IsAlive)
            {
                Console.WriteLine("Weak referance is alive");
            }
            else
            {
                Console.WriteLine("Weak referance was deleted");
            }

            Console.WriteLine("Total Memory after weak referance deleting: {0}", GC.GetTotalMemory(false));
        }
예제 #2
0
        static void Main(string[] args)
        {
            string WritePath    = @"./info/text.json";
            string WritePathXml = @"./info/text.xml";

            try
            {
                City     c      = new City();
                City     c1     = new City("Ukraine", "Kyiv Region", "Vishgorod");
                City     c2     = new City(c1);
                CityList c_list = new CityList();
                c_list.addCity(c.name_c);
                c_list.addCity(c1.name_c);
                c_list.addCity(c2.name_c);
                c_list.destroyCity(c1.name_c);


                c_list.lookupCities();
                Console.WriteLine("");
                Console.WriteLine("LIST WITH ENUMERATE INTERFACE:");
                Console.WriteLine("==============");

                Transformer[] transArray = new Transformer[4]
                {
                    new Transformer()
                    {
                        name = "Transformer John", velocity = 165
                    },
                    new Transformer()
                    {
                        name = "Transformer  Jim", velocity = 155
                    },
                    new Transformer()
                    {
                        name = "Transformer  Sue", velocity = 145
                    },
                    new Transformer()
                    {
                        name = "Transformer  Sue", velocity = 121
                    }
                };

                TransformerList transList = new TransformerList(transArray);
                foreach (Transformer p in transList)
                {
                    Console.WriteLine(p.name + " " + p.velocity);
                }

                Console.WriteLine("LIST WITH ICOMPATIBLE INTERFACE:");

                var transf = new List <Transformer>
                {
                    new Transformer()
                    {
                        name = "Transformer John", velocity = 165
                    },
                    new Transformer()
                    {
                        name = "Transformer  Jim", velocity = 155
                    },
                    new Transformer()
                    {
                        name = "Transformer  Sue", velocity = 145
                    },
                    new Transformer()
                    {
                        name = "Transformer  Sue", velocity = 121
                    }
                };
                transf.Sort();
                foreach (Transformer p in transf)
                {
                    Console.WriteLine("name: " + p.name + ", " + "velocity: " + p.velocity);
                }
                Console.WriteLine("===================");
                Console.WriteLine("");

                Transformer        tr   = new Transformer(new City(), 0, 0, "Slide", 100, 41);
                OperateTransformer city = new OperateTransformer();
                city.killTransformer("Bee");
                city.newProtokolTransformer(tr);
                city.CompareTo(4);

                List <Transformer> copy = city.getTransformers();

                Console.WriteLine("Army of transformers just of " + copy.NumOfTrans() + " members.");
                ///////////
                Console.WriteLine("\nJSON SERIALIZE\n");
                JsonSerializerOptions op = new JsonSerializerOptions
                {
                    WriteIndented = true,
                };

                using (StreamWriter sw = new StreamWriter(WritePath, false, System.Text.Encoding.Default))
                {
                    sw.WriteLine(JsonSerializer.Serialize <List <Transformer> >(transf, op));
                }
                using (StreamReader sr = new StreamReader(WritePath, System.Text.Encoding.Default))
                {
                    string txt  = null;
                    string line = null;
                    while ((line = sr.ReadLine()) != null)
                    {
                        txt = txt + line;
                    }
                    List <Transformer> t = JsonSerializer.Deserialize <List <Transformer> >(txt, op);
                    Console.WriteLine("    NAME       " + " " + "AGE" + " " + "VELOCITY");
                    foreach (Transformer i in t)
                    {
                        Console.WriteLine(i.Name + " " + i.Age + "    " + i.Vel);
                    }
                }
                ////////////////////////
                Console.WriteLine("\nXML SERIALIZE\n");
                XmlSerializer formatter = new XmlSerializer(typeof(List <Transformer>));
                using (FileStream fs = new FileStream(WritePathXml, FileMode.OpenOrCreate))
                {
                    formatter.Serialize(fs, transf);
                }

                using (FileStream fs = new FileStream(WritePathXml, FileMode.OpenOrCreate))
                {
                    List <Transformer> t = (List <Transformer>)formatter.Deserialize(fs);
                    Console.WriteLine("    NAME       " + " " + "AGE" + " " + "VELOCITY");
                    foreach (Transformer i in t)
                    {
                        Console.WriteLine(i.Name + " " + i.Age + "    " + i.Vel);
                    }
                }
                Console.Write("\n");
                /////////////////////////////////////////////////////////////////////
                Hunter <Transformer> hunt = new Hunter <Transformer> {
                    name = "Turuk", targets = transf, velocity = 1000, strength = 2
                };
                if (hunt.velocity > tr.velocity)
                {
                    Console.WriteLine(tr.name + " was killed by hunter " + hunt.name);
                }
            }
            catch (ExceptionFind ex)
            {
                Console.WriteLine($"Error occured: {ex.Message}{ex.Value}");
            }
            finally
            {
                Console.WriteLine("");
                Console.WriteLine("Fin");
            }
        }