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

            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(book);
            books.Add(new Book());

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

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

            Console.WriteLine();

            Console.WriteLine("Generics + Constraints");

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

            Console.WriteLine($"Has value? {number.HasValue}");
            Console.WriteLine($"Value: {number.GetValueOrDefault()}");
            Console.WriteLine();

            number = new Nullable <int>();
            Console.WriteLine($"Has value? {number.HasValue}");
            Console.WriteLine($"Value: {number.GetValueOrDefault()}");

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

            /*
             * When using generic list, we don't have to create many lists for different types, we simply pass the type and without any
             * casting or boxing, at runtime, our list is of the given type. We could use an object list, because object is a parent class
             * for all the types in C#, but that requires boxing or casting which is heavy on performance.
             * In practice we rarely have to create a generic.
             */

            var numbers = new GenericList <int>();

            numbers.Add(10);

            var books = new GenericList <Book>();

            books.Add(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());

            var number2 = new Nullable <int>();

            Console.WriteLine("Has Value? " + number2.HasValue);
            Console.WriteLine("Value: " + number2.GetValueOrDefault());
        }
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "111", 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 dictonary = new GenericDictionary <string, Book>();

            dictonary.Add("2222", new Book());


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

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

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

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

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

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

            // Using Nullable
            var number2 = new Nullable <int>();

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

            // This class is already available in System
            //   System.Nullable<T>
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var book = new Book {
                Isbn = "1111", Title = "C# Advanced"
            };

            //var numbers = new List(); instead of creating these different classes we can create a single generic class as below
            //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());

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

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

            var number1 = new Nullable <int>();

            Console.WriteLine("Has Value ?" + number1.HasValue);
            Console.WriteLine("Value: " + number1.GetValueOrDefault());
        }
Exemple #6
0
        static void Main(string[] args)
        {
            var book = new Book
            {
                ISBN  = "1234",
                Title = "Chris"
            };

            var numbers = new GenericList <int>();

            numbers.Add(1);

            var books = new GenericList <Book>();

            books.Add(book);

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

            dictionary.Add("1234", book);


            var number = new Nullable <int>();

            System.Console.WriteLine("Has value ? " + number.HasValue);
            System.Console.WriteLine("Value " + number.GetValueOrDefault());
        }
Exemple #7
0
        private static void GenericsExmaple()
        {
            var film = new Film()
            {
                Title = "Jaws", Id = "121"
            };

            //var films = new FilmList();
            //films.Add(film);

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

            // Creating a generic list is more performant as its created at run time, there is no casting or boxing and more code resuability.

            var numnbers = new GenericList <int>();

            numnbers.Add(1);

            var films = new GenericList <Film>();

            films.Add(film);

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

            dictionary.Add(123, new Film());
        }
Exemple #8
0
        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());


            // nullable is already apart of .net framework
            var number = new Nullable <int>(5);

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


            var number2 = new Nullable <int>();

            System.Console.WriteLine("Has Value? " + number2.HasValue);
            System.Console.WriteLine("Value: " + number2.GetValueOrDefault());
        }
        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(book);

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

            dictionary.Add("1234", book);

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

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



            Console.ReadKey();
        }
Exemple #10
0
        private static void GenericDictionaryExample()
        {
            var genericDictionary = new GenericDictionary();

            genericDictionary.Run();

            Console.WriteLine("EOF");
            Console.ReadKey();
        }
        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());
        }
Exemple #12
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();
        }
Exemple #13
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());
        }
Exemple #14
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()}");
        }
Exemple #15
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 #16
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());
        }
Exemple #17
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());
        }
        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 #19
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 #20
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 #21
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());
        }
        static void Main(string[] args)
        {
            var dictionary = new GenericDictionary <string, Book>();

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