Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to quickly cast an object in lambda in C#

I find out I have to cast things very often.

    Button.MouseMove += (s, e) =>
    {
        Drawable d = s as Drawable;
        d.Cursor = Cursors.Pointer;
    };

How do I reduce these three lines into one line like this??

    Button.MouseMove += (s, e) => s.Cursor = Cursors.Pointer where s as Drawable; //an example of how i want to simplify the code
        

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This works with at least C# 7.1:

Button.MouseMove += (s, e) => _ = s is Drawable d ? d.Cursor = Cursors.Pointer : default;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading