示例#1
0
 static void Delegate_Example_3()
 {
     // An example of a Regular Method; Anonymous Method, and Lambda Delegates
     // Which are all the same.
     Console.WriteLine("Delegate Example 3 - Showing Different/Same Delegates ");
     GenericExamples.Same_Main();
     Console.ReadKey();
 }
示例#2
0
        public static void Menu()
        {
            int x = 0;

            do
            {
                /*
                 * Interfaces Implement Polymorphism.   Each class that Inherits an Interfaces will implement
                 * the method defined in the interface in a different way, making it Polymorphic.
                 * Interfaces make it easier to make code easier to maintain,extend, and test.
                 * When a class implements an Interface, it fufills a contract set by the Interface.
                 *
                 * See Generic Examples also
                 *
                 * Interfaces should be programmed at the right level.   Interfaces should be granular
                 * IList<T> - ICollection  -> IENumberable
                 *
                 * IEnumerable Implementations (IEnumerable<T> limited to collectsions that are strongly typed:
                 *  List<T>
                 *  Array
                 *  ArrayList
                 *  SortedList<TKey, TValue>
                 *  HashTable
                 *  Queue / Queue<T>
                 *  Stack / Stack<T>
                 *  Dictionary<TKey, TValue>
                 *  ObservableCollection<T>
                 *
                 *  ICollection<T> Implementations
                 *  List<T>
                 *  SortedList<TKey, TValue>
                 *  Dictionary<TKey, TValue>
                 *
                 *  IList<T> Implementations
                 *  List<T>
                 *
                 *  Program at the Right Level
                 *  IEnumerable <T>  -  If we need to Iterate over a Collection / Sequence or Data Bind
                 *                      to a List Control
                 *  ICollection<T>  - If we need to Add/Remove Items in a Collection,
                 *                    Count Items in a Collection,
                 *                    Clear a Collection.
                 *  IList<T> -        If we need to Control the Order Items in a Collection,
                 *                    Get an Item by the Index.
                 *
                 *  Differences between IEnumerable, ICollection, and IList Interfaces
                 *  https://www.c-sharpcorner.com/UploadFile/78607b/difference-between-ienumerable-icollection-and-ilist-interf/
                 *
                 *  Abstract Classes        versus Interfaces
                 *  -------------------------  |  ---------------------
                 *  May Contain implementation |  May not contain implementation code - Biggest weakness
                 *  Code or may simulate an    |  By default all objects are public and absract.  Since it is
                 *  interface and just have    |  abstract by default then all methods must be implemented.
                 *  abstract signature.        |
                 *  A class may inherit from a |  A class may implement any number of interfaces - Biggest
                 *  single base class          |  strength.
                 *  Members have access        |  Members are automatically public
                 *  modifiers
                 *  May contain fields,        |  May only contain properties, methods, events, and indexers .
                 |  No implementation usually just contain signatures.
                 |  properties, constructors,  |
                 |  destructors, methods,      |  Interfaces may not contain fields
                 |  events, and indexers       |
                 |
                 |  Note:  You can use interface when you know signature but you don't know the implmentation.
                 |  You can use abstract class when you know some implementation but not all.  A class may only
                 |  inherit from one abstract class, but a class may inherit from multiple interfaces.  See
                 |  info on Abstract Methods in BaseExamples.cs
                 |
                 |  Note_2: Interfaces can't have static methods.  A class that implements an interface needs
                 |  to implement them all as instance methods.
                 |
                 |  As a guideline:
                 |  Use classes and subclasses for types that naturally share an implementation
                 |  Use interfaces for types that have independent implementations.
                 |
                 |  Natural - Can Compare with other Instances of the same type
                 |
                 |                EQUALITY                   COMPARISONS
                 | Natural        IEquatable<T>              IComparable<T>
                 |                                           IComparable
                 | Plugged_in     IEqualityComparer          IComparer
                 |                IEqualityComparer<T>       IComparer<T>
                 | Structural     IStructuralEquatable       IStructuralEquatable
                 |
                 |
                 |
                 |
                 */
                Console.Clear();
                Console.WriteLine(" Types of Interfaces \n ");
                Console.WriteLine(" 0.  IComparable \n ");
                Console.WriteLine(" 1.  IDisposable \n ");
                Console.WriteLine(" 2.  IEnumerable \n ");
                Console.WriteLine(" 3.  IList \n");
                Console.WriteLine(" 4.  IDictionary \n");
                Console.WriteLine(" 5.  Self Created \n");
                Console.WriteLine(" 6.  IQueryable \n");
                Console.WriteLine(" 8.  Web Page \n");
                Console.WriteLine(" 9.  Quit            \n\n ");
                Console.Write(" Enter Number to execute Routine ");


                int selection;
                selection = Common.readInt("Enter Number to Execute Routine : ", 0, 9);
                switch (selection)
                {
                case 0:
                    /*
                     * IComparable will allow you to compare one class( one instance) to another
                     * class (or instance).  You have to implement 'compare' in class that is
                     * inheriting IComparable.
                     *
                     * IComparable will tell you if
                     * x < y
                     * x > y
                     * x == y
                     * returning an integer
                     * - 1 means less than
                     *  1 means greater
                     *  0 means equal to
                     *
                     */

                    SortingExamples.Sort_A_Class();
                    Console.ReadKey();
                    SortingExamples.Sort_Numbers();
                    Console.ReadKey();
                    break;

                // ==, != operators  - Work out of the box only for primitive types and reference types
                //                     Operators do not work in generic code
                //                     Object.Equals  - ok in generic code
                //
                // >, <, >=, <=  operators - Work out of the box only for primitive types
                //
                case 2:
                    /*
                     * When something is define as IEnumerable it lets you know that you can use
                     * LINQ to Query.
                     *
                     * IEnumerable - A List, Array, Query, String implements IEnumerable.
                     * An IEnumerable Interface specifies that the underlying type implements IEnumerable
                     *
                     * public Interface IEnumerator
                     * {
                     ***
                     ***  MoveNext will throw an exception if collection has changed.
                     ***  Cannot modify a collection while enumerating.
                     ***
                     ***
                     *** bool MoveNext();
                     *** object Current {get;}
                     *** void Reset();
                     ***}
                     ***
                     ***public interface IEnumerable
                     ***{
                     ***IEnumerator GetEnumerator();
                     ***}
                     ***
                     **
                     ** You can implement a class that uses the IEnumerable<string> interface.  If you do this
                     ** you will have to use the 'yield return'
                     ** 'yield return' statements are NOT Return statements.
                     **
                     **  What happens when a yield return occurs inside a method that returns an IEnumerator interface
                     **  The C# compiler determines this is a case where you want the compiler to write an enumerator for
                     **  you.  The yield return statements simply tell the compiler what the sequence of enumeration values
                     **  to return is.
                     *
                     **  Use 'yield return' when you want to return elements from a collection one at a time. Once
                     *   execution goes back it will pick up execution at the statement immediately following
                     *   the 'yield' return.
                     *
                     *
                     * See also IterationStatements.cs for examples of Yield Return.
                     * See also LINQExamples.cs on how deferred execution works.
                     *
                     *
                     *
                     */
                    MenuCreateAndUseTypes.EnumerableMethods.Menu();
                    MenuCreateAndUseTypes.IEnumeratorInterface.Menu();
                    break;

                case 3:
                    // See BaseExamples.cs on more info on Array versus Lists
                    //
                    // IList<T>
                    // Arrays are a fixed size.  System.Array does not contain a definition for 'Add'
                    // new string[]  is a fixed size.
                    // This problem with arrays is what List<T> was designed to overcome.
                    // new List<string> allows you to add to an array.
                    // Arrays can be multi-dimensional.  List are restricted to being one dimensional.
                    //
                    // List<T> acts as an array in every aspect and in addition you can add and
                    // remove elements.
                    //
                    // List<T> uses a capacity and a count method
                    // Capacity - Gets increased if you keep adding elements.  Once you add elements
                    //            and go past the capacity it gets increased by 8.
                    //
                    // count - Total number of elements you have added
                    // if the count is 4 and the capacity is 8 you cannot display elements that exceed the count.
                    //

                    GenericExamples.GA_Main();
                    break;

                //case 4:
                //Dictionary uses a hash table.   It is a large collection
                //that will divide a collection into smaller collections.
                //Each smaller collection is defined as a bucket.
                //If you know what bucket the item your searching for is in,
                //you only need to search that bucket.
                //
                //The modulo % method along with the .gethashcode method is used to determine the bucket when
                //storing or retrieving a value.
                //
                //If two values x and y evaluate to equal then they must evaluate to the same hash code.
                //
                // StringComparer implements IEqualityComparer
                // If two objects are equal they must return the same hash code that is why the IEqualityComparer
                // defines its own get hash code method so you can make sure that rule is followed.
                //
                // public int GetHashCode (string obj)
                // {
                //    return obj.ToUpper().GetHashCode();   // Best to reuse what Microsoft has already done
                // }
                //
                // SortedList<TKey, TValue>  - Dictionary that keeps its values sorted.  It's a Dictionary and NOT a List.
                // Sorted List does NOT contain a Hash Table.  So when you implement an IComparer interface you won't have a
                // hash method.
                //
                // SortedDictionary<TKey, TValue>  - Functionally is the same as SortedList<TKey, TValue>
                // Added and Removing Elments is a lot faster using a Sorted Dictionary verus a Sorted List.
                // So only use a SortedDictionary if you need to make frequent changes to the collection.
                //
                case 5:
                    Shape shape = new Oval();
                    shape.WhatAmI();
                    var car = new Car();
                    car.Start();
                    car.Stop();
                    var mower = new LawnMower();
                    mower.Start();
                    mower.Stop();
                    Console.Write("\nPress Enter to Continue ");
                    Console.ReadKey();

                    Collections.
                    Using_Collections();
                    Console.ReadKey();
                    break;

                case 6:
                    /* IQueryable is used in Entity Frameworks and serves as a replacement for IEnumerable.  It is how
                     * a database is accessed via SQL.  Like IEnumerable it represents a datasource something you can query.
                     * IQueryable does not execute code in memory.  IQueryable translates code into SQL.   IQueryable works
                     * with the Expression clause.  The Expression Clause will translate Lambda statements and passes them
                     * so that they can be translated into SQL.  The Entity Framework will inspect the Expression to tranlate
                     * into SQL.
                     *
                     * Watch this video again at some point to write program that will use Entity Frameworks and display
                     * SQL to Console.
                     * https://app.pluralsight.com/course-player?clipId=64018007-2730-4598-9fca-6aee3d4e0fcd
                     */
                    Console.WriteLine(" Not yet built");
                    Console.ReadKey();
                    break;

                case 8:
                    Process.Start("http://msdn.microsoft.com/en-us/library/87d83y5b.aspx");
                    break;

                case 9: x = 9;
                    break;

                default: Console.WriteLine(" Invalid Number");
                    break;
                }
            } while (x < 9);  // end do
        }  // end Menu()
示例#3
0
        public static void Menu()
        {
            int x = 0;

            do
            {
                Console.Clear();
                Console.WriteLine(" Create Types \r ");
                Console.WriteLine(" Create value types (structs, enum), refererence types, generic types, \r");
                Console.WriteLine(" constructors, static variables, methods, classes, extension methods, \r");
                Console.WriteLine(" optional and named parameters, and indexed properties; create \r");
                Console.WriteLine(" overloaded and overridden methods \n");
                Console.WriteLine(" 0.  Value Types (Immutable) \n ");
                Console.WriteLine(" 1.  Generic Types \n ");
                Console.WriteLine(" 2.  Nulls  \n ");
                Console.WriteLine(" 3.  Static Variables \n ");
                Console.WriteLine(" 4.  Extension Methods\n ");
                Console.WriteLine(" 5.  Optional/Named/Out Parameters \n ");
                Console.WriteLine(" 6.  Methods \n");
                Console.WriteLine(" 7.  Indexers \n");
                Console.WriteLine(" 8.  Reference Types (Mutable) (Can be boxed) \n");
                Console.WriteLine(" 9.  Quit            \n\n ");

                int selection;
                selection = Common.readInt("Enter Number to Execute Routine : ", 0, 9);
                switch (selection)
                {
                case 0: ValueTypes.Menu();
                    break;

                case 1: GenericExamples.PrimaryMain();
                    DynamicExamples.DMain();
                    break;

                case 2: Nulls.Menu();
                    break;

                case 3: StaticExamples.Menu();
                    Console.ReadKey();
                    break;

                case 4:
                    /* Extension Methods
                     * - Extend types without altering them.  Escpecially useful for extending types that are not
                     *   yours or are sealed.
                     * - They are declared as static in a static class
                     * - The first paramter has the 'this' modifier
                     * - The first parameter's type is the type being extended
                     *
                     * Both the class and the method have to be static
                     * The type that your extending goes into the signature as the first argument with the 'this' keyword
                     */
                    EnumerableMethods.Menu();
                    break;

                case 5:
                    /* Optional/Named/Out Parameters
                     *
                     * ***************************************************************************************
                     * Optional Parameters
                     * There are 4 ways that can be used to make method parameters optional
                     * 1. Use parameter arrays - Can have 0 or 1,2,3,... parameters.
                     * 2. Method overloading
                     * 3. Specify parameter defaults
                     * 4. Use OptionalAttribute that is present in System.Runtime.InteropServices namespace
                     *
                     * Please note - A parameter array must be the last parameter in a formal parameter list.
                     *
                     * 1. Parameter arrays
                     *
                     * AddNumbers(10,20)  <-- 3 or more paramters optional
                     * AddNumbers(10,20, new object[] {30,40,50});
                     * public static void AddNumbers( int firstNumber, int secondNumber, params object[] resetofNumbers)
                     *
                     * params must be last parameter - below will get comile error
                     * public static void AddNumbers( int firstNumber,  params object[] resetofNumbers, int secondNumber)
                     * https://www.youtube.com/watch?v=jbtjGii300k&index=67&list=PLAC325451207E3105
                     *
                     * 2. Method OverLoading
                     *
                     * AddNumber(10,20)  <-- Will give error, since you must provide 3 or more paramters.
                     * AddNumber(10,20,null)  <-- will work without error
                     * public static void AddNumbers(int firstNumber, int secondNumber, int[] restofNumbers)
                     *
                     * AddNumber(10,20)  <-- Will work with the below method overload
                     * public static void AddNumbers(int firstNumber, int secondNumber)
                     * {
                     *   AddNumbers(fistNumber, secondNumber, null);
                     * }
                     * public static void AddNumbers(int firstNumber, int secondNumber, int[] restOfNumbers)
                     * {
                     * }
                     *
                     * https://www.youtube.com/watch?v=khcOI3-Kh84&index=68&list=PLAC325451207E3105
                     *
                     * 3. Specifying parameter defaults - If we don't supply the third parameter then it
                     *    will take the default assigned).  Optional parameters should always be placed as
                     *    the last parameter(s).
                     *
                     * AddNumbers(10,20);  <-- 3rd parameter will take default of null.
                     * AddNumbers(10, 20, new int[] {30, 40});  <-- will use 30, 40 as 3rd and 4th parameters.
                     * public static void AddNumbers(int firstNumber, int secondNumber, int[] restofNumbers = null)
                     *
                     *
                     * https://www.youtube.com/watch?v=Dmycz0ro1Yc&index=69&list=PLAC325451207E3105
                     *
                     * 4. Optional Attribute - Present in System.Runtime.InteropServices namespace
                     *
                     * using System.Runtime.InteropServices
                     * AddNumbers(10,20);
                     * public static void AddNumbers(int firstNumber, int secondNumber, [Optional] restofNumbers)
                     *
                     *                          *
                     * https://www.youtube.com/watch?v=p_9f5SSXxLw&list=PLAC325451207E3105&index=70
                     *
                     *
                     * ***************************************************************************************
                     * Named Parameters
                     *   You don't have to specify the type.  The name and type is attained from the
                     *   calling method.  You don't have to worry about the order, since the parameters
                     *   are named it is smart enough to figure them out regardless of the order.
                     *
                     *   Sample
                     *   var customer = new Customer();
                     *   var order = new Order();
                     *   var payment = new Payment();
                     *   orderController.PlaceOrder
                     *   (customer, order, payment, allowSplitOrders:true, emailReceipt:false)
                     *
                     * **************************************************************************************
                     * Out Parameters
                     *   The 'out' keyword causes arguments to be passed by reference.  This is like
                     *   the 'ref' keyword, except that the ref requires that the variable be
                     *   initialized before it is passed.
                     *
                     *   To use both the ref and out parameter, both the
                     *   method definition and the calling method must explicitly use the
                     *   ref or out keyword
                     *
                     *   In general ref parameters are not recommended.  Ref parameters are not intuitive, why did you have
                     *   to pass an empty string (assuming you initialized with empty string).  Ref parameters make the code
                     *   harder to understand.
                     *
                     *   Out Parameters are also not recommeneded.  Even though you don't have to initialize, they too are
                     *   not intutive.  They also make the method harder to understand.
                     *
                     *   Tuples is a data structure that allows for the use of multiple
                     *   elements.  The .Net framework class does not represent a tuple but rather provides methods for
                     *   creating tuples.
                     *
                     *   Tuple is a nice way to group a set of data and pass it out.  However it is very clumbsy to work with
                     *   and it not intuitive at all in what is being returned.
                     *
                     *   So since Ref, out, and Tuples are not intuitive.  Is there a better way to return parameters.  Yes
                     *   and it will through objects.  An 'object' can readily be returned from a method.  You can often
                     *   define a class just for return values.   The term 'object' refers to the instance of a standardized
                     *   class.   The class is usually defined with a 'get' and 'set' properties.
                     *
                     */
                    RunwithOutput();
                    break;

                case 6: Methods.Menu();
                    break;

                case 7:
                    ExamLibrary.CreateAndUseTypes.Indexer.Menu();
                    Console.ReadKey();
                    break;

                case 8: ReferenceTypes.Menu();
                    Console.ReadKey();
                    break;

                case 9: x = 9;
                    break;

                default: Console.WriteLine(" Invalid Number");
                    break;
                }
            } while (x < 9);
        }