-
MY PROJECTS
-
Recent Posts
- SpringBoot. Exception Management
- Java.Hibernate.JoinTableAnnotation
- SpringBoot.Making our first starter and autoconfiguration
- Spring. Creating main annotation to start business logic
- Spring.Reading from properties file
- Spring.How to define spring version inside springBoot ?
- SpringBoot App inside Docker
- Kafka.FirstExperience
- Docker.MySql and Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0
- Gradle.Tips
- Spring.AppConfig
- Leetcode.Best-time-to-buy-and-sell-stock
- LeetCode.ClimbStairs.Fibbonacci
- Leetcode.Roman-to-integer
- LeetCode.Palindrome-number
- Java.DesignPatterns.Lightweight
- Java.DesignPatterns.Proxy
- Java.DesignPatterns.Facade
- Java.DesignPatterns.Composite
- Java.DesignPatterns.Bridge
Categories
- Aptana
- Azure
- C#
- DataSnap
- DBExpress
- Delphi
- Delphi и сети
- Delphi. Язык программирования
- ExtJS
- FastReport
- FireDAC
- FireMonkey
- GIT
- ICS
- IDE
- IIS
- Indy
- InnoSetup
- javascript
- jQuery
- JSON
- LiveBindings
- MSHTML
- MySQL
- PHP
- REST
- Ribbons
- SMS
- SQL инструкции
- SVN
- TRichView
- UniGui
- WebBroker
- WinAPI
- Windows
- Алгоритмы
- Без рубрики
- Деревья
- Ищу ответ
- Компонентостроение
- Мои компоненты
- Начальный уровень
- Обработка исключений
- Парсинг
- Потоки(Threads)
- Регулярные выражения
- Тестирование приложений
Author Archives: Stanislav_Panteleev
C#. Linq and Non generic collections, using OfType
Main idea is to use OfType<T> to cast to generic variant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Collections.Generic; using System.Collections; // << non generic using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqAndNonGenericCollections { class Program { static void Main(string[] args) { LinqOverArrayList(); Console.ReadLine(); } static void LinqOverArrayList() { ArrayList cars = new ArrayList() // << non generic { new Car {PetName="BMW",Color="1234",Speed=10,Make=123456 }, new Car {PetName="Lada",Color="1234",Speed=20,Make=123456 }, new Car {PetName="Mitsubishi",Color="1234",Speed=30,Make=123456 }, new Car {PetName="Ferrari",Color="1234",Speed=40,Make=123456 } }; // transform ArrayList in type compatible to IEnumerable<T> var carsEnum = cars.OfType<Car>(); // <<< !!! var fastCars = from c in carsEnum where c.Speed > 20 select c; foreach (var car in fastCars) { Console.WriteLine(car.PetName); } } } class Car { public string PetName { get; set; } public string Color { get; set; } public int Speed { get; set; } public int Make { get; set; } } } |
Posted in C#
Comments Off on C#. Linq and Non generic collections, using OfType
C#. LinqAndCollections
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqAndCollections { class Program { static void Main(string[] args) { List<Car> Cars = new List<Car>() { new Car {PetName="BMW",Color="1234",Speed=10,Make=123456 }, new Car {PetName="Lada",Color="1234",Speed=20,Make=123456 }, new Car {PetName="Mitsubishi",Color="1234",Speed=30,Make=123456 }, new Car {PetName="Ferrari",Color="1234",Speed=40,Make=123456 } }; GetFastCars(Cars); Console.WriteLine(" "); GetFastFerrari(Cars); Console.ReadLine(); } static void GetFastCars(List<Car> Cars) { var fastCars = from c in Cars where c.Speed > 20 select c; foreach (var car in fastCars) Console.WriteLine(car.PetName); } static void GetFastFerrari(List<Car> Cars) { var fastCarsBMW = from c in Cars where c.Speed > 20 && c.PetName == "Ferrari" select c; foreach (var car in fastCarsBMW) Console.WriteLine(car.PetName); } } class Car { public string PetName { get; set; } public string Color { get; set; } public int Speed { get; set; } public int Make { get; set; } } } |
Posted in C#
Comments Off on C#. LinqAndCollections
C#. Linq and Arrays. Simple Examples
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace linqSimpleArray { class Program { static void Main(string[] args) { QueryOverStrings(); Console.ReadLine(); } static void QueryOverStrings() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside IEnumerable<string> subset = from g in games where g.Contains(" ") orderby g select g; // the same through implicit tipization var subset2 = from g in games where g.Contains(" ") orderby g select g; // linq operator will be implemented here foreach (string s in subset) Console.WriteLine("Item: {0}", s); Console.WriteLine("123"); foreach (string s in subset2) Console.WriteLine("Item: {0}", s); //------------- immediate execution of linq int[] numbers = { 10,20,30,40,1,2,3 }; int[] subarray = (from i in numbers where i<10 select i).ToArray<int>(); // executed here foreach (int j in subarray) Console.WriteLine("Item: {0}", j); // or //for (int j = 0; j < subarray.Length; j++) { Console.WriteLine("Item: {0}", subarray[j]); } LinqReturnExamples.GetStringSubset(); LinqReturnExamples.GetStringSubsetAsArray(); } } class LinqReturnExamples { public static IEnumerable<string> GetStringSubset() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside IEnumerable<string> subset = from g in games where g.Contains(" ") orderby g select g; return subset; } public static string[] GetStringSubsetAsArray() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside string[] subarray = (from g in games where g.Length < 10 select g).ToArray<string>(); // executed here return subarray; } } } |
Posted in C#
Comments Off on C#. Linq and Arrays. Simple Examples
C#. ANONYMOUS TYPES. IMPLICIT CLASS OBJECTS CREATION And Unsafe Code
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnonymousTypes { class Program { static void Main(string[] args) { // implicitly creating class, only for LINQ // in normal situation create classes normally var myCar = new { Color = "Bright", Make = "Saab", Speed = 55 }; Console.WriteLine("My car is a {0} {1}",myCar.Color,myCar.Make); ReflectAnonymousType(myCar); PrintValueAndAdresses(); Console.ReadLine(); } static void ReflectAnonymousType(object obj) { Console.WriteLine("obj is an instance of {0}:",obj.GetType().Name); // continued in Troelsen, p. 452 } unsafe static void PrintValueAndAdresses() { int i; int* ptrToMyInt = &i; *ptrToMyInt = 123; Console.WriteLine("Value of i {0}",i); Console.WriteLine("Adress of ptrToMyInt {0}", (int)&ptrToMyInt); } } } |
Posted in C#
Comments Off on C#. ANONYMOUS TYPES. IMPLICIT CLASS OBJECTS CREATION And Unsafe Code
C#. LinqExample
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExamples { class Program { static void Main(string[] args) { QueryOverStrings(); } static void QueryOverStrings() { string[] strings = { "123", "123 4", "1234 5" }; //linq implementation IEnumerable<string> subset = from s in strings where s.Contains(" ") orderby s select s; foreach (string s in subset) Console.WriteLine("Item {0}",s); Console.ReadLine(); } } } |
Posted in C#
Comments Off on C#. LinqExample
C#. Anonymous types. Implicit class objects creation
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnonymousTypes { class Program { static void Main(string[] args) { // implicitly creating class, only for LINQ // in normal situation create classes normally var myCar = new { Color = "Bright", Make = "Saab", Speed = 55 }; Console.WriteLine("My car is a {0} {1}",myCar.Color,myCar.Make); ReflectAnonymousType(myCar); PrintValueAndAdresses(); Console.ReadLine(); } static void ReflectAnonymousType(object obj) { Console.WriteLine("obj is an instance of {0}:",obj.GetType().Name); // continued in Troelsen, p. 452 } unsafe static void PrintValueAndAdresses() { int i; int* ptrToMyInt = &i; *ptrToMyInt = 123; Console.WriteLine("Value of i {0}",i); Console.WriteLine("Adress of ptrToMyInt {0}", (int)&ptrToMyInt); } } } |
Posted in C#
Comments Off on C#. Anonymous types. Implicit class objects creation
C#. UserTypeCast
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UserConverts { class Program { static void Main(string[] args) { int a = 123; long b = a; // not explicit int c=(int)b;// explicit convert, I say to compiler "I know ehat am I doing" Base someBase; someBase = new Derived(); // not explicit Derived someDerived = (Derived)(someBase); // explicit Rectangle r = new Rectangle(15,4); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine(); //converting Rectangle to square using explicit Square s = (Square)r; Console.WriteLine(r.ToString()); s.Draw(); Console.WriteLine(""); //converting Square to Rectangle using implicit Rectangle r2 = (Rectangle)s; Console.WriteLine(r2); r2.Draw(); Console.ReadLine(); } class Base { } class Derived : Base { } } public class Rectangle { public int Width { get; set; } public int Height { get; set; } public Rectangle(int w, int h) { Width = w; Height = h; } public Rectangle() { } public void Draw() { for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { Console.Write("*"); } Console.WriteLine(); } } public override string ToString() { return string.Format("Width {0} Height {1} ",Width,Height); } public static implicit operator Rectangle(Square s) { Rectangle r = new Rectangle(); r.Height = s.Length; r.Width = 2 * s.Length; return r; } } public class Square { public int Length { get; set; } public Square(int l) { Length = l; } public Square() { } public void Draw() { for (int i = 0; i < Length; i++) { for (int j = 0; j < Length; j++) { Console.Write("*"); } Console.WriteLine(); } } // EXPLICIT RECTANGLE TO SQUARE public static explicit operator Square(Rectangle r) { Square s = new Square(); s.Length = r.Height; return s; } } } |
Posted in C#
Comments Off on C#. UserTypeCast
C#. StringIndexators
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringIndexators { class Program { static void Main(string[] args) { // PeopleCollection behave like array !!! PeopleCollection2 people2 = new PeopleCollection2(); people2["123"] = new Person(123, "123", "123"); people2["1234"] = new Person(123, "1234", "123"); people2["12345"] = new Person(123, "12345", "123"); people2["123456"] = new Person(123, "123456", "123"); Person somePerson = people2["123"]; Console.WriteLine(somePerson.FirstName); Console.ReadLine(); } } public class Person { public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } public override string ToString() { return ("Age=" + Age); // return base.ToString(); } } public class PeopleCollection : IEnumerable { private ArrayList ArrayListPeople = new ArrayList(); public Person GetPerson(int pos) { return (Person)ArrayListPeople[pos]; } public void AddPerson(Person p) { ArrayListPeople.Add(p); } public void ClearPeople() { ArrayListPeople.Clear(); } // foreach support public IEnumerator GetEnumerator() { return ArrayListPeople.GetEnumerator(); } public int Count { get { return ArrayListPeople.Count; } } // adding indexator - then collection will have behaviour like array public Person this[int index] { get { return (Person)ArrayListPeople[index]; } set { ArrayListPeople.Insert(index, value); } } } public class PeopleCollection2 : IEnumerable { private Dictionary<string, Person> dictPeople = new Dictionary<string, Person>(); // adding indexator - then collection will have behaviour like array public Person this[string name] { get { return (Person)dictPeople[name]; } set { dictPeople[name] = value; } } public void ClearPeople() { dictPeople.Clear(); } public int Count { get { return dictPeople.Count; } } // foreach support public IEnumerator GetEnumerator() { return dictPeople.GetEnumerator(); } } } |
Posted in C#
Comments Off on C#. StringIndexators
C#. Indexators Custom collection behave like array
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Indexators { class Program { static void Main(string[] args) { // PeopleCollection behave like array !!! PeopleCollection people = new PeopleCollection(); people[0] = new Person(123, "123", "123"); people[1] = new Person(123, "123", "123"); people[2] = new Person(123, "123", "123"); people[3] = new Person(123, "123", "123"); for (int i = 0; i < people.Count; i++) { Console.WriteLine(people[i].FirstName); } Console.ReadLine(); } } public class Person { public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } public override string ToString() { return ("Age=" + Age); // return base.ToString(); } } public class PeopleCollection : IEnumerable { private ArrayList ArrayListPeople = new ArrayList(); public Person GetPerson(int pos) { return (Person)ArrayListPeople[pos]; } public void AddPerson(Person p) { ArrayListPeople.Add(p); } public void ClearPeople() { ArrayListPeople.Clear(); } // foreach support public IEnumerator GetEnumerator() { return ArrayListPeople.GetEnumerator(); } public int Count { get { return ArrayListPeople.Count; } } // adding indexator - then collection will have behaviour like array public Person this[int index] { get { return (Person)ArrayListPeople[index]; } set { ArrayListPeople.Insert(index, value); } } } } |
Posted in C#
Comments Off on C#. Indexators Custom collection behave like array
C#. Delegates.AnonymousMethods and LambdaExpression
123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnonymMethods { class Program { static void Main(string[] args) { int localInt = 123; SomeClass sc = new SomeClass(); sc.SetDelegate(SomeHandler); SomeClass.SomeDelegate d = new SomeClass.SomeDelegate(SomeEventHandler); sc.SomeEvent1 += d; // assigning anonymous methods with or without params sc.SomeEvent1 += delegate { Console.WriteLine("Anonymous method "); }; // << works without param - why? sc.SomeEvent1 += delegate(string msg) { Console.WriteLine("Anonymous method "+msg); }; sc.SomeEvent1 += delegate { Console.WriteLine("Anonymous method with local var "+localInt); }; // << works without param - why? // lambda expression - like alias for above examples sc.SomeEvent1 += (string msg) => { Console.WriteLine("Lambda expression " + msg); }; // start testing... sc.SomeDelegateMethod(); sc.SomeEventMethod(); Console.ReadLine(); } public static void SomeHandler(string Aparam) { Console.WriteLine(Aparam); } public static void SomeEventHandler(string Aparam) { Console.WriteLine(Aparam); } } class SomeClass { // delegate public delegate void SomeDelegate(string Aparam); private SomeDelegate listOfHandlers; public void SetDelegate(SomeDelegate method) { listOfHandlers += method; } //event public event SomeDelegate SomeEvent1; // firing methods public void SomeDelegateMethod() { if (listOfHandlers != null) { listOfHandlers("someDelegateMessage"); } } public void SomeEventMethod() { if (SomeEvent1 != null) { SomeEvent1("someEventMessage"); } } } } |
Posted in C#
Comments Off on C#. Delegates.AnonymousMethods and LambdaExpression