Esempio n. 1
0
        static void Main(string[] args)
        {
            var book = new Book()
            {
                Isbn = "123321", Title = "Test"
            };
            GenericList <Book> books = new GenericList <Book>();

            books.Add(new Book());
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var nonGeneric = new NonGenericList();

            nonGeneric.Add(1);

            var generic = new GenericList <int>();

            generic.Add(1);

            var generic2 = new GenericList <string>();

            generic2.Add("test");
        }
Esempio n. 4
0
        public void CallGenericList()
        {
            GenericList <int> intlist = new GenericList <int>();

            intlist.Add(1);

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

            stringlist.Add("string");

            GenericList <ExampleClass> exampleClass = new GenericList <ExampleClass>();

            exampleClass.Add(new ExampleClass());
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var booklist = new BookList();

            booklist.Add(new Book());

            var numList = new GenericList <int>();

            numList.Add(1);

            var genBook = new GenericList <Book>();

            genBook.Add(new Book());
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            NormalObj normalObj = new NormalObj {
                Id = 1, Description = "A"
            };

            //Need 2 seperate lists
            var numbers = new List <int>();

            numbers.Add(10);

            var normalList = new NormalList();

            normalList.Add(normalObj);

            //Can use one generic list
            //Reusable, No performance penality
            var genericListInt = new GenericList <int>();

            genericListInt.Add(10);

            var genericListNormalObj = new GenericList <NormalObj>();

            genericListNormalObj.Add(normalObj);

            //.NET already has generic collections so there's no need to create our own
            //see System.Collections.Generic
            ICollection <int> A = new List <int>();
            IEnumerable <int> B = new List <int>();
            IList <int>       C = new List <int>();
            //etc

            //Can also make generic objects
            //Also called generic dictionnaries
            GenericObj <int, int, string> intInt = new GenericObj <int, int, string> {
                Id = 1, Description = 1
            };
            GenericObj <int, string, string> intString = new GenericObj <int, string, string> {
                Id = 1, Description = "Description"
            };

            //Value type example
            var valueType     = new ValueTypeConstraint <int>(1);
            var valueTypeNull = new ValueTypeConstraint <int>();

            Console.WriteLine(valueType.GetValueOrDefault().ToString());
            Console.WriteLine(valueTypeNull.GetValueOrDefault().ToString());
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var numbers = new GenericList <int>();

            numbers.Add(10);

            var book = new BookList();

            var books = new GenericList <BookList>();

            books.Add(book);

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

            dictionary.Add("1234", new Book());
        }
Esempio n. 8
0
        //types of contraints

        //where T : IComparable
        //where T : Product
        //where T : struct
        //where T : class
        //where T : new()

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

            var books = new GenericList <Book>();

            books.Add(book);

            Console.WriteLine(books.GetSize());
            Console.WriteLine(books[0].Title);

            books[0] = new Book()
            {
                ISBN = "22", Title = "New Book"
            };
            var newBook = books.Get(0);

            Console.WriteLine(newBook.Title + " " + newBook.ISBN);

            //multiple generic parameters
            var dictionary = new GenericDictionary <string, Book>();

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

            //using egeneric method
            Console.WriteLine(Utilities.Max <int>(5, 10));

            //contraint to paticular class
            var calculator = new DicountCalculator <Product>();
            var product    = new Product()
            {
                Price = 12.99, Name = "TV"
            };
            var discount = calculator.CalculateDiscount(product);

            Console.WriteLine("Discount: $" + discount);

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

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

            Console.ReadKey();
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            var book = new Books()
            {
                Isbn = "1111", Title = "The Call of the Wild", Price = 67
            };
            var numbers = new GenericList <int>();

            numbers.Add(5);
            var dict = new GenericDictionary <string, Books>();

            dict.Add("3323", new Books());
            var num = new Nullable <int>(5);

            Console.WriteLine(" has a value? " + num.HasValue);
            Console.WriteLine(" value itself: " + num.GetValueOrDefault());
        }
Esempio n. 10
0
        static void Main()
        {
            // Declare a list of type int.
            GenericList <int> list1 = new GenericList <int>();

            list1.Add(1);

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

            list2.Add("");

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

            list3.Add(new ExampleClass());
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            var numbers = new GenericList <int>();

            numbers.Add(1);

            var books = new GenericList <Book>();

            books.Add(new Book());

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

            dictionary.Add("123asd", new Book());

            var number1 = new Nullable <int>();

            Console.WriteLine($"Has value: {number1.HasValue} - Value : {number1.GetValueorDefault()}");
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            //Declare a list of type int.
            GenericList <int> intList = new GenericList <int>();

            intList.Add(1);

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

            stringList.Add("item1");

            //Declare list of type MyClass1
            GenericList <MyClass1> myClassList = new GenericList <MyClass1>();

            myClassList.Add(new MyClass1());
            Console.ReadKey();
        }
Esempio n. 13
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());
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            var numbers = new GenericList <int>();

            numbers.Add(10);
            //int a = numbers[0];

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

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

            Console.WriteLine("has Value ? " + num.HasValue);
            Console.WriteLine("Value: " + num.GetValueOrDefault());

            var num1 = new System.Nullable <int>(5);

            Console.WriteLine("has Value ? " + num1.HasValue);
            Console.WriteLine("Value: " + num1.GetValueOrDefault());
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            // Checking the 1st Impleentation of Generic Class
            var numbers = new GenericList <int>();

            numbers.Add(10);
            //Checking Nullable using Generics
            var valued    = new Nullable <int>(10);
            var notValued = new Nullable <double>();

            Console.WriteLine("Valued Has Value: " + valued.HasValue);
            Console.WriteLine("Valued Value: " + valued.GetValueOrDefault());
            Console.WriteLine("notValued Has Value: " + notValued.HasValue);
            Console.WriteLine("notValued Value: " + notValued.GetValueOrDefault());
            bool?res = null;

            Console.WriteLine("Boolean Value is : {0}", res.GetValueOrDefault());
            Console.ReadLine();
        }
Esempio n. 16
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());
        }
Esempio n. 17
0
        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());
        }
Esempio n. 18
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());
        }
Esempio n. 19
0
        static void Main(string[] args)
        {
            GenericList <int> list = new GenericList <int>();

            Point p1 = new Point();
            Point p2 = new Point();
            GenericList <Point> pList = new GenericList <Point>();

            pList.Add(p1);
            pList.Add(p2);

            Console.WriteLine(pList[1]);
            Console.WriteLine(list.Count);
            Console.WriteLine(list.Capacity);

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);


            Console.WriteLine(list.Count);
            Console.WriteLine(list.Capacity);



            list.Add(1);

            list.InsertAt(1, -50);

            list.RemoveAt(list.Count - 1);

            Console.WriteLine(list.ToString());

            Console.WriteLine(list.IndexOf(5));

            Console.WriteLine(pList.ToString());

            Console.WriteLine(list.Max());
            Console.WriteLine(list.Min());
            //Console.WriteLine(pList.Max());
            Console.WriteLine(list.Count);
            Console.WriteLine(list.Capacity);
        }
Esempio n. 20
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();
        }
Esempio n. 21
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());
        }
Esempio n. 22
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());
        }
Esempio n. 23
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
        }