Exemple #1
0
        static void Main(string[] args)
        {
            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(new Book());

            var dictionary = new GenericDictionary <string, Book>();

            dictionary.Add("1234", new Book());

            var number = new Nullable <int>(5);

            Console.WriteLine("has value ?" + number.HasValue);
            Console.WriteLine("value: " + number.GetValueOrDefault());
        }
Exemple #2
0
        static void Main()
        {
            var someList = new GenericList <double>(4);

            for (int i = 0; i < 5; i++)
            {
                someList.AddElement(i + 2.5);
            }

            someList.InsertAtIndex(0, -12.5);
            someList.InsertAtIndex(0, -16.3);

            for (int i = 0; i < someList.Count; i++)
            {
                Console.WriteLine(someList[i]);
            }

            someList.Clear();
            Console.WriteLine(someList.Count);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            GenericList <int> test = new GenericList <int>(3);

            test.Add(1);
            test.Add(2);
            test.Add(3);
            test.Add(4);
            test.Add(5);
            test.Insert(1, 33);
            test.Insert(0, 22);
            test.Insert(6, 66);
            Console.WriteLine("List:");
            Console.WriteLine(test);
            Console.WriteLine("Min: {0}", test.Min());
            Console.WriteLine("Max: {0}", test.Max());
            test.RemoveAt(4);
            Console.WriteLine(test);
            Console.WriteLine("Position of '1' = {0}", test.IndexOf(1));
            Console.WriteLine("Position of '33' = {0}", test.IndexOf(33));
        }
        static void Main(string[] args)
        {
            //List<string> sehirler = new List<string>();//Bu generic bir class, c# dilinde default yer alan bir method List as a generic.
            //Console.WriteLine(sehirler.Count);//bu default programda Count tanımlı olduğu için hiçbirşey yapmadan ilerleriz.
            //sehirler.Add("Osmaniye");         //ancak yeni bir generic tanımladığımız zaman, orada Count'u aşağıdaki gibi tanımlarız ;
            //Console.WriteLine(sehirler.Count);
            //Console.WriteLine("----------------");

            GenericList <string> sehirler3 = new GenericList <string>();//Yeni bir generic class oluşturup, 34'teki gibi class tipine T ve methodda T tipinde bir takma adlı veri ekleterek,

            //burada İstanbul'u string olarak ekledik.
            Console.WriteLine(sehirler3.Count);
            sehirler3.Add2("İstanbul");
            Console.WriteLine(sehirler3.Count);//Count yazabilmek için bir Count property'si tanımladık, yalnızca get olarak. property çalıştığında arraylenght okuyup, Count adı altında bir int olarak kaydetecek.
            sehirler3.Add2("İzmir");
            sehirler3.Add2("Ankara");
            sehirler3.Add2("Bursa");
            sehirler3.Add2("Konya");
            sehirler3.Add2("Kayseri");
            Console.WriteLine(sehirler3.Count);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "1234", Title = "C# Book"
            };

            // System.Collections.Generic.

            var books = new GenericList <Book>();

            books.Add(book);

            var dictionary = new GenericDictionary <string, Book>();

            dictionary.Add("1", new Book());


            var number = new Nullable <int>(5);

            Console.WriteLine("Has value ? " + number.HasValue + " " + number.GetValueOrDefault());
        }
Exemple #6
0
        public static void Main()
        {
            // int is the type argument
            GenericList <int> list = new GenericList <int>();

            for (int x = 0; x < 10; x++)
            {
                list.AddHead(x);
            }

            foreach (int i in list)
            {
                System.Console.Write(i + " ");
            }
            System.Console.WriteLine("\nDone");

            // Declare a list of type string
            GenericList <string> list2 = new GenericList <string>();

            // Declare a list of type ExampleClass
            GenericList <DelegateTestExampleClass> list3 = new GenericList <DelegateTestExampleClass>();

            Node4 <string> n4 = new Node4 <string>("hello ", 4);
            Node5 <Node4 <string>, Node5 <int, int> > n5 =
                new Node5 <Node4 <string>, Node5 <int, int> >(
                    new Node4 <string>("hello", 5), new Node5 <int, int>(6, 7));

            NodeItem <DelegateTestExampleClass> strNode = new NodeItem <DelegateTestExampleClass>();

            SwapTest();

            List <int> intList1 = new List <int>();

            AddToList1(intList1);

            DelegateTest();
            DefaultTest();
            OutTest();
        }
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "53434", Title = "C#"
            };

            Console.WriteLine(book.Isbn);
            Console.WriteLine(book.Title);

            //The problem here is we have two different types of Lists
            //var number = new List();
            //number.Add(19);

            //var books= new Book();
            //books.Add(book);

            var numbers = new GenericList <int>();

            numbers.Add(100);

            var books = new GenericList <Book>();

            books.Add(book);

            //Use of Dictionary
            //As you see we pass anything we want as parameters types
            //So if we want to use it again we can change just the parameter types
            var dictionary = new GenericDictionary <string, int>();

            dictionary.Add("Ben Hur", 42);


            //If we have a value in our parameter we will get it else we get 0
            //Try to leave empty the parameter
            var number = new Nullable <int>(5);

            Console.WriteLine("Has value? " + number.HasValue);
            Console.WriteLine("Value:" + number.GetValueorDefault());
        }
Exemple #8
0
        static void Main(string[] args)
        {
            //Create generic class with int
            GenericList <int> listInt = new GenericList <int>();

            listInt.AddHead(1);
            listInt.AddHead(2);
            listInt.AddHead(3);
            listInt.AddHead(4);

            System.Console.Write("Stack of integer: ");
            foreach (int i in listInt)
            {
                System.Console.Write(i + " ");
            }

            System.Console.WriteLine("\n");


            //Create generic class with String

            GenericList <string> listString = new GenericList <string>();

            listString.AddHead("A");
            listString.AddHead("B");
            listString.AddHead("C");
            listString.AddHead("D");

            System.Console.Write("Stack of string: ");
            foreach (string i in listString)
            {
                System.Console.Write(i + " ");
            }


            System.Console.WriteLine("\n\nDone.");
            Console.ReadKey();
        }
Exemple #9
0
        static void Main(string[] args)
        {
            // For Not Generics examples: we need to instance each list
            Book book = new Book {
                Isbn = "1234567", Title = "C# Advanced"
            };                                                               // instance of the Book object
            Del


            List numbers = new List();  // instance of List

            numbers.Add(10);


            //// For Generics examples: we do not need to instance each list

            var intNum = new GenericList <int>(); // in here, specify the type, and it did not have to specify the type earlier

            intNum.Add(20);

            var books0 = new GenericList <Book>();// object Genericlist is list of books, it is not a list of objects

            books0.Add(new Book());



            //// Generic DiscountCalculator

            var discountCalculator = new DiscountCalculator <Product>();


            // Generic Nullable
            var number = new Nullable <int>(100);

            Console.WriteLine(number);
            Console.WriteLine(number.HasValue);
            Console.WriteLine(number.GetValueOrDefault());
        }
Exemple #10
0
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "1111", Title = "C# Advanced"
            };

            //Trocamos esses dois por uma lista de Genericos
            //var numbers = new List();
            //numbers.Add(10);

            //var books = new Booklist();
            //books.Add(book);

            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(new Book());

            //Genericos em .NET são encontrados nas classes da 'System.Collections.Generic'.

            //Agora os dictionary.

            var dictionary = new GenericDictionary <string, Book>();

            dictionary.Add("1234", new Book());

            var number = new Nullable <int>(5);

            Console.WriteLine("Has value ? " + number.HasValue);
            Console.WriteLine("Value: " + number.GetValueOrDefault());

            Console.WriteLine("Press a key to close...");
            Console.ReadKey();
        }
Exemple #11
0
        //Generic lists are very rarely used in applications. Probably won't need to create your own, but instead
        //just use ones that exist.

        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "1111", Title = "C# advanced"
            };

            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(new Book());

            var dictionary = new GenericDictionary <string, Book>();

            dictionary.Add("1234", new Book());


            var number = new Nullable <int>();

            Console.WriteLine("Has Value ?" + number.HasValue);
            Console.WriteLine("Value: " + number.GetValueOrDefault());
        }
Exemple #12
0
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "1111", Title = "C# Advanced"
            };

            //var numbers = new List();
            //numbers.Add(10);

            //var books = new BookList();
            //books.Add(book);

            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(new Book());

            var dictionary = new GenericDictionary <string, Book>();

            dictionary.Add("1234", new Book());
        }
Exemple #13
0
        static void Main(string[] args)
        {
            // Generic list
            GenericList <string> Lista = new GenericList <string>(5);


            // Add element
            Lista.AddElement("Victor");
            Lista.AddElement("Ana");
            Lista.AddElement("Ioana");
            Lista.AddElement("Emilia");
            Lista.AddElement("Mina");
            Console.WriteLine(Lista);
            Console.WriteLine();

            // Access element by index
            var x = Lista[2];

            Console.WriteLine("Elementul de pe indexul {0} are valoarea {1}.", 2, x);
            Console.WriteLine();

            // Remove element by index
            Lista.RemoveElement(3);
            Console.Write("Dupa ce am sters elementul de pe indexul 3: ");
            Lista.Print();
            Console.WriteLine();

            // Insert element at given position
            Lista.InsertElement(2, "Robert");
            Console.Write("Dupa ce am inserat elementul Robert pe indexul 2: ");
            Console.WriteLine(Lista);
            Console.WriteLine();

            ////Clear the list
            //Lista.ClearList();
            //Console.WriteLine(Lista);
            //Console.WriteLine();

            // Find element by its value
            Console.WriteLine($"Elementul \"Robert\" se afla pe indexul {Lista.IndexOf("Robert")}.");
            Console.WriteLine();

            // ToString()
            Console.Write("Elementele sirului (folosind ToString) sunt: ");
            Console.WriteLine(Lista);
            Console.WriteLine();

            //         Problem 2. Auto-grow
            Lista.AutoGrow();
            Console.Write("AutoGrow: ");
            Console.WriteLine(Lista);
            Console.WriteLine();

            //       Problem 3. Min and Max
            var min = Lista.Min();

            Console.WriteLine("Minimul sirului este {0}.", min);
            Console.WriteLine();

            var max = Lista.Max();

            Console.WriteLine("Maximul sirului este {0}.", max);
            Console.WriteLine();


            Console.ReadKey();
        }
Exemple #14
0
        static void Main(string[] args)
        {
#if badcode
            var numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            //Length
            Console.WriteLine("Length of Array is: " + numbers.Length);

            //IndexOf
            var index = Array.IndexOf(numbers, 9);
            Console.WriteLine("Index of 9 is: " + index);

            //Clear


            var book = new Book {
                Isbn = "1111", Title = "C# Basic"
            };
            var book1 = new Book {
                Isbn = "2222", Title = "C# Intermediate"
            };
            var book2 = new Book {
                Isbn = "3333", Title = "C# Advanced"
            };


            var number = new Custom_List();
            number.Add(10);

            foreach (var item in numbers)
            {
                number.Add(item);
            }

            foreach (var item in number)
            {
                Console.WriteLine(item);
            }

            var books = new BookList();
            books.Add(book);
            books.Add(book1);
            books.Add(book2);

            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(books[i].Title);
            }

            var number1 = new GenericList <int>();
            foreach (var item in numbers)
            {
                number1.Add(item);
            }
            number1.Add(10);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(number1[i]);
            }

            var books1 = new GenericList <Book>();
            books1.Add(book);
            books1.Add(book1);
            books1.Add(book2);

            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(books1[i].Title);
            }
#endif
        }