static void Main(string[] args) { MyCollection<int> intCollection = new MyCollection<int>(2, 6); intCollection.Add(3); for(int i=0;i<intCollection.Count();i++) Console.WriteLine(intCollection[i]); intCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < intCollection.Count(); i++) Console.WriteLine(intCollection[i]); int newInt = intCollection.CreateEntity(); Console.WriteLine("создали число "+newInt); //коллекция котов MyCollection<Cat> catCollection = new MyCollection<Cat>(); catCollection.Add(new Cat { Name = "Мурка", Weight = 8 }); catCollection.Add(new Cat { Name = "Мурзик", Weight = 15 }); catCollection.Add(new Cat { Name = "Барсик", Weight = 10 }); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); catCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); foreach (Cat cat in catCollection) Console.WriteLine(cat); Cat newCat = catCollection.CreateEntity(); Console.WriteLine("Создали кота "+newCat); DateTime time1 = DateTime.Now; ArrayList arrList = new ArrayList(); for (int i = 0; i < 1000000;i++ ) { arrList.Add(i);//упаковка int x =(int) arrList[i];//распаковка } Console.WriteLine((DateTime.Now-time1).TotalSeconds); arrList.Add("Вася"); DateTime time2 = DateTime.Now; List<int> list = new List<int>(); for (int i = 0; i < 1000000; i++) { list.Add(i); int x = list[i]; } //list.Add("Вася"); Console.WriteLine((DateTime.Now - time2).TotalSeconds); Console.ReadKey(); }
private void BtnExitTest_Click(object sender, EventArgs e) { while (_outForChecking.Count() > 0) { var test = _outForChecking[0]; _outForChecking.Remove(test); _submittedTests.Add(test); } Flush(); }
public void WeakEnumValues() { //**************************************** var Found = false; //**************************************** AllocateWithSingle <object>(out var MyCollection, out var MyObject, 1); GC.Collect(); // Should find only one object foreach (var MyInnerObject in MyCollection.ToStrong()) { if (object.ReferenceEquals(MyObject, MyInnerObject)) { Found = true; continue; } Assert.Fail("Unknown Object: {0}", MyInnerObject); } Assert.IsTrue(Found, "Object 1 not found"); Assert.AreEqual(1, MyCollection.Count(), "Item was not removed"); GC.KeepAlive(MyObject); }
public void EnumValues() { //**************************************** bool Found1 = false, Found2 = false; //**************************************** AllocateWithDual <object>(out var MyCollection, out var MyObject1, out var MyObject2, 0); GC.Collect(); // Should find two objects foreach (var MyObject in MyCollection.ToStrong()) { if (object.ReferenceEquals(MyObject, MyObject1)) { Found1 = true; continue; } if (object.ReferenceEquals(MyObject, MyObject2)) { Found2 = true; continue; } Assert.Fail("Unknown Object"); } Assert.AreEqual(2, MyCollection.Count(), "Items were not added"); Assert.IsTrue(Found1, "Object 1 not found"); Assert.IsTrue(Found2, "Object 2 not found"); GC.KeepAlive(MyObject1); GC.KeepAlive(MyObject2); }
public void ShouldWriteListOfEnumerableOfRandomTypeCorrectly() { var values = new MyCollection <object>(new object[] { 1, new[] { 2, 3 }, 'a' }); // Given var writerMachine = CreateWriterMachine(); // When var writer = writerMachine.Writer(); writer.Write(values); // When var readerMachine = CreateReaderMachine(writerMachine.GetOutput()); var reader = readerMachine.Reader(); reader.ReadStructSignature().Should().Be((byte)(PackStream.TinyList | values.Count())); reader.ReadLong().Should().Be(1); reader.ReadStructSignature().Should().Be((byte)(PackStream.TinyList | 2)); reader.ReadLong().Should().Be(2); reader.ReadLong().Should().Be(3); reader.ReadStructSignature().Should().Be(PackStream.TinyString | 1); reader.ReadLong().Should().Be(97); }
public void WeakAddRange() { AllocateRange <object>(out var MyCollection, 2); GC.Collect(); Assert.AreEqual(0, MyCollection.Count(), "Items were not removed"); }
public void WeakAdd() { AllocateRange <object>(out var MyCollection, 1); GC.Collect(); Assert.AreEqual(0, MyCollection.Count(), "Item was not expired"); }
public void WeakAddSingle() { AllocateWithSingle <object>(out var MyCollection, out var MyObject, 0); GC.Collect(); Assert.AreEqual(1, MyCollection.Count(), "Item was not removed"); GC.KeepAlive(MyObject); }
public void RemoveMulti() { AllocateWithDual <object>(out var MyCollection, out var MyObject1, out var MyObject2, 0); GC.Collect(); Assert.AreEqual(2, MyCollection.Count(), "Items were not added"); MyCollection.Remove(MyObject1); Assert.AreEqual(1, MyCollection.Count(), "Item was not removed"); GC.KeepAlive(MyObject1); GC.KeepAlive(MyObject2); }
private void Flush() { listBoxSubmitted.DataSource = null; listBoxSubmitted.DataSource = _submittedTests; if (_submittedTests.Count() > 0) { listBoxSubmitted.DisplayMember = "Name"; listBoxSubmitted.ValueMember = "Name"; } listBoxOutForChecking.DataSource = null; listBoxOutForChecking.DataSource = _outForChecking; if (_outForChecking.Count() > 0) { listBoxOutForChecking.DisplayMember = "Name"; listBoxOutForChecking.ValueMember = "Name"; } }
public void ShouldWriteListOfEnumerableTypeCorrectly() { var nums = new MyCollection <int>(new[] { 1, 2, 3 }); // Given var writerMachine = CreateWriterMachine(); // When var writer = writerMachine.Writer(); writer.Write(nums); // When var readerMachine = CreateReaderMachine(writerMachine.GetOutput()); var reader = readerMachine.Reader(); reader.ReadStructSignature().Should().Be((byte)(PackStream.TinyList | nums.Count())); reader.ReadLong().Should().Be(1); reader.ReadLong().Should().Be(2); reader.ReadLong().Should().Be(3); }
static void Main() { #region Создание коллекции и вывод ее на экран циклом Foreach с указанием моего пользовательского типа Console.WriteLine("Добавляю в мою коллекцию элементы"); var collection = new MyCollection <IMyClass> { new SumTwoNumber(23, 56), new SumTwoNumber(55, 90), new SumTwoNumber(155, 190), new PositiveDiffirentTwoNumber(45, 190), new PositiveDiffirentTwoNumber(555, 678) }; // При указании типа в Foreach в итераторе обращаюсь к методу T Current и возвращ. свой тип данных foreach (IMyClass item in collection) { Console.WriteLine($"В коллекции элемент со значениями: {item.Field1} и {item.Field2}. {item}"); } #endregion DeleteItemFromCollection(null); #region Добавление в ранее созданную коллекцию 1 дополнительного элемента и вывод ее на экран циклом Foreach с применением распаковки var testElement = new PositiveDiffirentTwoNumber(-200, 80); Console.WriteLine(); Console.WriteLine($"Добавляю еще один элемент в коллекцию со значениями: {testElement.Field1} и {testElement.Field2}"); collection.Add(testElement); // При указании var в Foreach в итераторе обращаюсь к методу Object Current и здесь делаю Unboxing. foreach (var item in collection) { Console.WriteLine($"В коллекции элементы со значениями: { ((IMyClass)item).Field1 } и {((IMyClass)item).Field2}. {(IMyClass)item}"); } #endregion #region Удаление элемента и вывод на экран с помощью переопределенного метода ToString Console.WriteLine(); Console.WriteLine("Удаляю только что добавленный элемент из коллекции"); DeleteItemFromCollection(testElement); Console.WriteLine(); // При указании var в Foreach в итераторе обращаюсь к методу Object Current и здесь надо Unboxing делать. В данном примере я переопределил ToString foreach (var item in collection) { Console.WriteLine($"{item}"); } #endregion #region Задание 2. Работа с Generic Console.WriteLine(); Console.WriteLine("Пример работы с Generic"); var testGeneric = new TestGenericClass <int, IMyClass>(testElement) { FieldT = 5555 }; Console.WriteLine($"Generic: {testGeneric} "); Console.WriteLine($"MyClass: {testGeneric.GetMyClass()}"); #endregion Console.ReadKey(); void DeleteItemFromCollection(IMyClass elem) { //Попытка удаления элемента из коллекции try { Console.WriteLine(); Console.WriteLine($"Сейчас в коллекции {collection.Count()} элементов. Пробую удалить"); collection.Delete(elem); } catch (Exception e) { Console.WriteLine($"Ошибка: {e.GetBaseException().Message}"); } } }