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) { 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(); }