© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
121 replies
Bobby Bob

(Concept Question) Delegates and Lambda Expressions [Answered]

I'm so confused about the concept of Delegates and Lambda Expression

namespace LINQ;

internal static class Program
{
    static void Main()
    {
        var titanic = new Movie("Titanic", 1998, 4.5f);
        PrettyPrint(titanic, (movie) => new string('*', (int)movie.Rating)); // "Lambda Expression"
    }

    private static void PrettyPrint(Movie movie, Func<Movie, string> printRating)
    {    
        // var rating = printRating.Invoke(movie);
        // Or simply
        var rating = printRating(movie);
        
        Console.WriteLine($"Name: {movie.Name}, Rating: [{rating}]");
    }
    
    // Note: This is "expression body"
    private static int RandomMethod() => 20;
}

class Movie
{
    public Movie(string name, int releaseYear, float rating)
    {
        Name = name;
        ReleaseYear = releaseYear;
        Rating = rating;
    }

    public string Name { get; set; }

    public int ReleaseYear { get; set; }

    public float Rating { get; set; }
}
namespace LINQ;

internal static class Program
{
    static void Main()
    {
        var titanic = new Movie("Titanic", 1998, 4.5f);
        PrettyPrint(titanic, (movie) => new string('*', (int)movie.Rating)); // "Lambda Expression"
    }

    private static void PrettyPrint(Movie movie, Func<Movie, string> printRating)
    {    
        // var rating = printRating.Invoke(movie);
        // Or simply
        var rating = printRating(movie);
        
        Console.WriteLine($"Name: {movie.Name}, Rating: [{rating}]");
    }
    
    // Note: This is "expression body"
    private static int RandomMethod() => 20;
}

class Movie
{
    public Movie(string name, int releaseYear, float rating)
    {
        Name = name;
        ReleaseYear = releaseYear;
        Rating = rating;
    }

    public string Name { get; set; }

    public int ReleaseYear { get; set; }

    public float Rating { get; set; }
}


Specifically this code
(movie) => new string('*', (int)movie.Rating)
(movie) => new string('*', (int)movie.Rating)


Is this suppose to create a new method in place of (movie)? I looked through the lowered C# code on sharplab but it's even more confusing o-0
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

First time using delegates/lambda expressions
C#CC# / help
3y ago
Events and Delegates [Answered]
C#CC# / help
4y ago
Lambda, delegates, events. Help !
C#CC# / help
2y ago
Lambda, delegates, evens. Help !
C#CC# / help
2y ago