Beispiel #1
0
        static void Main(string[] args)
        {
            var movie = "Frozen";

            var moviePlayer = new MoviePlayer {
                CurrentMovie = movie
            };

            MoviePlayer.MovieFinishedHandler handler = PrintMovieOver;

            moviePlayer.MovieFinished += handler;

            moviePlayer.PlayMovie();
        }
Beispiel #2
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();
        }
Beispiel #3
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
        }
        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; };
        }