static void ShowExampleMyGClassR() { Console.WriteLine("*********** Задание 2 (вариант 2) ***************"); GenericsList <int> gList = new GenericsList <int>() { 1, 2, 4, 5, 6 }; GenericsUserClassR <int, GenericsList <int> > gUserClass = new GenericsUserClassR <int, GenericsList <int> >(gList); GenericsList <int> gNewList = new GenericsList <int>(); gUserClass.Element = 7; gNewList = gUserClass.AddElement(); Console.WriteLine("Элементы старой коллекции:"); foreach (object o in gList) { Console.WriteLine(o); } Console.WriteLine("Элементы новой коллекции:"); foreach (object o in gNewList) { Console.WriteLine(o); } }
private static void DinosaurTests() { Dinosaur dino1 = new Dinosaur(); dino1.Size = 10; // dino1.Eat(); TRex tRex = new TRex(); tRex.Size = 20; // tRex.Eat(); Dinosaur dino2 = new TRex(); // upcasting // dino2.Eat(); Dinosaur dino3 = new Pterodactyl(); // upcasting // dino3.Eat(); Dinosaur dino4 = new Dinosaur(); dino4 = (TRex)tRex; // dino4.Eat(); dino4 = (Pterodactyl)dino3; // dino4.Eat(); Dinosaur dino6 = dino3 as Pterodactyl; // This is a way to test if the object is valid dino6.Sleep(); TRex tRex3 = dino1 as TRex; dino1.Sleep(); Dinosaur[] dinoArray = { dino1, tRex, dino2, dino3 }; foreach (Dinosaur item in dinoArray) { if (item is TRex) //Is there an object with that name { item.Eat(); } if (item is Pterodactyl) { item.Sleep(); } } GenericsList <Dinosaur> dinoList = new GenericsList <Dinosaur>(); dinoList.Add(dino1); dinoList.Add(tRex); dinoList.Add(dino2); dinoList.Add(dino3); }
static void ShowExampleMyList() { Console.WriteLine("*********** Задание 1 ***************"); GenericsList <int> gList = new GenericsList <int>(); ArrayList arrayToFill = new ArrayList() { 1, 2, "asdad", 3, 4, 5, "asdas", 6, 7 }; Console.WriteLine("Добавление элементов в коллекцию MyList:"); for (int i = 0; i < arrayToFill.Count; i++) { try { gList.Add(arrayToFill[i]); Console.WriteLine($"Добавлен элемент {arrayToFill[i]}"); } catch (Exception e) { Console.WriteLine($"Ошибка при добавлении элемента {arrayToFill[i]}: " + e.Message); } } Console.WriteLine("\nЭлементы коллекции GList:"); foreach (object o in gList) { Console.WriteLine(o); } bool Rem = true; while (Rem) { //удалим заданный (по индексу) элемент Console.WriteLine("Введите индекс удаляемого элемента:"); if ((int.TryParse(Console.ReadLine(), out int Index)) && (Index > -1) && (Index < gList.Count)) { try { gList.Remove(Index); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("\nЭлементы GList:"); foreach (object o in gList) { Console.WriteLine(o); } }
public void GenericsList_AddingElementsError() { GenericsList <int> gList = new GenericsList <int>(); ArrayList arrayToFill = new ArrayList() { 1, 2, 3, "str", 5, 6, 7 }; for (int i = 0; i < arrayToFill.Count; i++) { gList.Add(arrayToFill[i]); } }
public void GenericsList_AddingElements() { GenericsList <int> gList = new GenericsList <int>(); ArrayList arrayToFill = new ArrayList() { 1, 2, 3, 4, 5, 6, 7 }; for (int i = 0; i < arrayToFill.Count; i++) { gList.Add(arrayToFill[i]); } Assert.AreEqual(arrayToFill.Count, gList.Count); }
public ViewModel() { #region -- Use Generics class and methods -- var book = new Book { Id = "1110", Name = "C# Advanced" }; var numbrers = new GenericsList <int>(); numbrers.Add(10); var books = new GenericsList <Book>(); books.Add(new Book() { Id = "1111", Name = "Titre n°2" }); // -- Generic Dictionary -- var dictionary = new GenericsDictionary <string, Book>(); dictionary.Add("1102", new Book() { Id = "1111", Name = "Titre n°2" }); // -- Use Generic value type -- var num = new Repositories.Objects_Generics.Nullable <int>(); Console.WriteLine("As est-elle une valeure ? " + num.HasValue); Console.WriteLine("New value = : " + num.HasValue); #endregion // -- Get All -- pController.GetPersons(); // -- -- Person person = new Person() { Id = 4, Name = "Chateau", Adresse = "Versailles Chantiers" }; string insert = pController.AddPerson(person); }
private static void Week4FundamentalsReview() { //Fundamentals /* * example of calling a static class and its static method, noting * that we do not need to instantiate an instance of the class to use it */ Console.WriteLine(Utility.AddTwoNumbers(7, 6)); //the square has access to the abstract class Shape's properties (ie: square.Sides) Square square = new Square(5, 5); square.Sides = 100; Console.WriteLine(square.Area()); //Generics example Dinosaur dino1G = new Dinosaur(); Dinosaur dino2G = new Dinosaur(); Dinosaur dino3G = new Dinosaur(); TRex tRexG = new TRex(); GenericsList <Dinosaur> dinoList = new GenericsList <Dinosaur>(); dinoList.Add(dino1G); dinoList.Add(dino2G); dinoList.Add(dino3G); dinoList.Add(tRexG); //Indexers IndexerSample indexSample = new IndexerSample(); for (int i = 0; i < indexSample.Length; i++) { Console.WriteLine(indexSample[i]); } }