Esempio n. 1
0
        static void Main(String[] args)
        {
            var moviePlayer = new MoviePlayer
            {
                CurrentMovie = Movie.StarWars4
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisc;

            //subscribe to an event
            moviePlayer.MovieFinished += handler;

            moviePlayer.MovieFinished += EjectDisc;

            //unsubscribe
            // moviePlayer.MovieFinished -= handler;
            moviePlayer.MovieFinished += () =>
            {
                // can do anything inside a lambda
                Console.WriteLine("handle event with block body");
            };

            // with expression body, you can only put one line in
            moviePlayer.MovieFinished += () => Console.WriteLine("expression body");

            // we can specify type on lamda function params
            // but usually they are inferred from context like var

            //moviePlayer.DiscEjected += (string s) => Console.WriteLine($"Ejecting {s}");
            moviePlayer.DiscEjected += s => Console.WriteLine($"Ejecting {s}");

            Console.WriteLine("Playing movie....");

            moviePlayer.Play();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer
            {
                CurrentMovie = Movie.StarWars4
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisc;

            // subscribe to an event
            moviePlayer.MovieFinished += handler;

            // unsubscribe
            // moviePlayer.MovieFinished -= handler; // will make it so we don't eject disk

            moviePlayer.MovieFinished += EjectDisc;

            moviePlayer.MovieFinished += () =>
            {
                Console.WriteLine("handle event with block body lambda");
            };

            moviePlayer.MovieFinished += () => Console.WriteLine("expression body");

            //Type is usually inferred from context so (string s) is unnecessary
            moviePlayer.DiscEjected += (string s) => Console.WriteLine($"Ejecting {s}");

            moviePlayer.Play();

            Console.ReadLine(); // will wait for me to press enter before exiting
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer
            {
                CurrentMovie = Movie.StarWars4
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisc;

            // subscribe to an event
            moviePlayer.MovieFinished += handler;

            // unsubscribe to an event
            //moviePlayer.MovieFinished -= handler;
            moviePlayer.MovieFinished += EjectDisc;
            moviePlayer.MovieFinished += () =>
            {
                Console.WriteLine("handle event with block-body lambda.");
            };

            // with expression body, you can only put one line in
            moviePlayer.MovieFinished += () => Console.WriteLine("expression body");

            moviePlayer.DiscEjected += s => Console.WriteLine($"Ejecting {s}");

            Console.WriteLine("Playing movie...");

            moviePlayer.Play();

            Console.ReadLine();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer();

            {
                Movies CurrentMovie = Movies.StarWars4;
            }

            moviePlayer.Play();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var movie = "Frozen";

            var moviePlayer = new MoviePlayer {
                CurrentMovie = movie
            };

            MoviePlayer.MovieFinishedHandler handler = PrintMovieOver;

            moviePlayer.MovieFinished += handler;

            moviePlayer.PlayMovie();
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var player = new MoviePlayer
            {
                CurrentMovie = new Movie
                {
                    Title       = "The Lion King",
                    ReleaseDate = new DateTime(2019, 7, 19)
                }
            };

            // we are treating methods as just another form of data
            // that can be in variables etc.
            MoviePlayer.MovieEndHandlerNoParam handler = AnnounceEndOfMovie;

            MoviePlayer.MovieEndHandlerWithTitle handler2 = AnnounceEndOfMovieByName;

            Action <string> handler3 = AnnounceEndOfMovieByName;

            // instead of declaring methods, i can use lambda expressions
            // to create functions in-line
            Action <string> handler4 = title => Console.WriteLine(title);

            Func <int, int, int> add = (a, b) => a + b;

            bool x = add(3, 4) == 7;

            // lambda expressions also allow "block body" like regular methods.
            Func <string, int> printLength = value =>
            {
                Console.WriteLine(value.Length);
                return(value.Length);
            };

            // subscribe to events with +=
            player.MovieEnd += handler4;
            player.MovieEnd += (title) => Console.WriteLine($"{title} is over from lambda.");
            //player.MovieEnd += handler2;
            //player.MovieEnd += handler2;

            //player.MovieEnd -= handler2; // unsubscribe with -=

            player.Play();

            Linq();
        }
        static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer
            {
                CurrentMovie = Movie.StarWars4
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisc;

            // subscribe to an event
            moviePlayer.MovieFinished += handler;

            // unsubscribe from an event
            //moviePlayer.MovieFinished -= handler;

            moviePlayer.MovieFinished += EjectDisc;

            moviePlayer.MovieFinished += () =>
            {
                //for (int i = 0; i < length; i++)
                //{

                //}
                //if ()
                //    if ()

                Console.WriteLine("handle event with block-body lambda.");
            };

            // with expression body, you can only put one line in
            moviePlayer.MovieFinished += () => Console.WriteLine("expression body");

            // we can specify type on lambda function parameters...
            // but usually, they are inferred from context (like "var" does).
            //moviePlayer.DiscEjected += (string s) => Console.WriteLine($"Ejecting {s}");

            moviePlayer.DiscEjected += s => Console.WriteLine($"Ejecting {s}");

            FuncAndAction();

            Console.WriteLine("Playing movie...");

            moviePlayer.Play();

            Console.ReadLine(); // wait for me to press enter before exiting
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            var movie = "Frozen";

            var moviePlayer = new MoviePlayer {
                CurrentMovie = movie
            };

            // this implicit conversion works because the method has the right shape.
            MoviePlayer.MovieFinishedHandler handler = PrintMovieOver; // referencing the method, not calling

            //moviePlayer.MovieFinished += handler; // subscribe with +=
            //moviePlayer.MovieFinished -= handler; // unsubscribe with -=

            moviePlayer.MovieFinished += PrintWhichMovieOver;

            moviePlayer.PlayMovie();
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            //var movie = new Movie();
            //movie.Name = "Infinity War";

            var movie = new Movie {
                Name = "Infinity War"
            };

            var moviePlayer = new MoviePlayer()
            {
                CurrentMovie = movie
            };

            // subscribe to the event:
            // first we need a function / method that should run
            // when the event occurs.

            // for the variable, the method itself is the value.
            // the type is, the delegate type defined in MoviePlayer.
            MoviePlayer.PlayFinishedHandler handler = EjectDisc;

            MoviePlayer.PlayFinishedHandlerWithName handler2 = EjectDisc;

            // subscribe to the event with +=
            // (why +=? because we are adding one function to a list of
            // other subscribing functions)
            //moviePlayer.PlayFinished += handler2;

            Action <string> handler3 = EjectDisc;

            moviePlayer.PlayFinished += handler3;

            Action <string> handler4 = name =>
            {
                Console.WriteLine("Handling with lambda expression");
            };

            moviePlayer.PlayFinished += handler4;

            //moviePlayer.PlayFinished -= handler; // unsubscribe

            moviePlayer.Play();
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var player = new MoviePlayer
            {
                Currentmovie = new Movie
                {
                    Title       = "The Lion King",
                    ReleaseDate = new DateTime(2019, 7, 19)
                }
            };

            //we are treating methods as just another form of data
            //that can be in variables etc.
            MoviePlayer.MovieEndHandlerNoParam handler = AnnounceEndOfMovie;

            MoviePlayer.MovieEndHandlerWithTitle handler2 = AnnounceEndOfMovieByName;

            Action <string> handler3 = AnnounceEndOfMovieByName;

            Action <string> handler4 = (title) => Console.WriteLine(title);

            Func <int, int, int> add = (a, b) => a + b;

            bool x = add(3, 4) == 7;

            Func <string, int> printLength = value =>
            {
                Console.WriteLine(value.Length);
                return(value.Length);
            };


            //subscribe with +=
            player.MovieEnd += handler2;

            //unsubscribe with -=
            player.MovieEnd -= handler2;
            player.MovieEnd += (title) => Console.WriteLine($"(title) is over from lambda");

            player.Play();
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer()
            {
                CurrentMovie = Movie.StarWars5
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisk;

            moviePlayer.MovieFinished += handler;

            Console.WriteLine("playing movie");

            moviePlayer.Play();

            moviePlayer.MovieFinished += () =>
            {
                Console.WriteLine("Lambda expression");
            };
        }
Esempio n. 12
0
        public static void Main(string[] args)
        {
            var moviePlayer = new MoviePlayer
            {
                CurrentMovie = Movie.StarWars4
            };

            MoviePlayer.MovieFinishedHandler handler = EjectDisc;

            // subscribe to an event
            moviePlayer.MovieFinished += handler;

            // unsubscribe to an event
            // moviePlayer.MovieFinished -= handler;

            moviePlayer.MovieFinished += () =>
            {
                Console.WriteLine("handle event with block-body lambda.");
            };

            moviePlayer.MovieFinished += () => Console.WriteLine("Expression body");

            //we can specify type on lambda funtion parameters
            // but usually they are inferred from context (like var does)

            moviePlayer.DiscEjected += s => Console.WriteLine($"Ejecting {s}");

            FuncAndAction();

            Console.WriteLine("Playing movie...");

            moviePlayer.Play();

            // wait for me to press enter before exiting
            Console.ReadLine();
        }
Esempio n. 13
0
        static void Main(string[] args)
        {
            var player = new MoviePlayer();

            player.CurrentMovie = "Star Wars";

            //player.Finished += DisplayFinishedMessage;
            //player.Finished += (
            //    (name) =>
            //    {
            //        int a = 3;
            //        a += 1;
            //        Console.WriteLine($"finished movie {name}.");
            //    }
            //);

            FinishedHandler handler = ((name) => Console.WriteLine($"finished movie {name}."));

            player.Finished += handler;

            player.Finished += (name) => Console.WriteLine("second handler too!");

            player.Finished -= handler;

            // handler would no longer be called

            player.Finished += handler;

            player.PlayMovie();

            // ////////////////////// LAMBDA EXPRESSIONS WITH LINQ

            var movieNames = new List <string>()
            {
                "Star Wars",
                "Toy Story",
                "Jurassic Park",
                "The Godfather",
                "The Avengers",
                "Cinderella"
            };

            //int maxLength = 0;
            //for (int i = 0; i < movieNames.Count; i++)
            //{
            //    if (movieNames[i].Length > maxLength)
            //    {

            //    }
            //}

            movieNames.Max(movieName => movieName.Length); // returns 13

            // first movie name that starts with a T
            movieNames.First(x => x[0] == 'T');

            // LINQ Language-Integrated Query Language
            // works on any IEnumerable<> or IQueryable<>

            var y = movieNames.Select(x => x[1]).ToList(); // get the second character of each movie name
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Linq();

            // object initialization syntax.
            // if no parens after MoviePlayer, zero-arg constructor "()" is assumed.
            var player = new MoviePlayer
            {
                CurrentMovie = "Lord of the Rings: The Fellowship of the Ring Extended Edition"
            };

            // the function must have a compatible signature
            // with the delegate of the event.
            MoviePlayer.MovieFinishedStringHandler handler = EjectDisc;

            // subscribe to events with +=
            player.MovieFinished += handler;
            // unsubscribe with -=
            //player.MovieFinished -= handler;
            // it's like you're appending to a list of functions.

            // when C# got generics, they added Func and Action generic classes.
            // and we can use these instead of delegate types.

            // Action is for void-return functions
            // Func is for non-void-return functions
            Action <string> handler2 = EjectDisc;

            //player.MovieFinished += handler2;

            // lambda expressions
            player.MovieFinished += s => Console.WriteLine("lambda subscribe");
            // this lambda takes in a string (inferred by compiler)
            // and returns nothing (because WriteLine returns nothing).
            // therefore it is compatible with that delegate type.
            // and we don't need to define a method like "EjectDisc".

            player.PlayMovie();

            // some func/action examples:

            // function taking int and string, returning bool:
            Func <int, string, bool> func = (num, str) => true;
            // the last type parameter is the return type,
            // and the ones before it are the arguments.

            // function taking zero arguments, returning bool:
            Func <bool> func2 = () => false;

            // function taking three arguments, returning void.
            Action <int, string, bool> func3 = (num, str, b) =>
            {
                if (b)
                {
                    Console.WriteLine(num);
                    Console.WriteLine(str);
                }
            };
            // lambdas can have a block body like methods

            // function taking bool, returning void.
            Action <bool> func4 = b => { return; };
        }