How can we take advantage of WindowsFroms composite pattern 2 – using LINQ

I change my last version to code using LINQ library:

public static class WinFormsExtension
{
    public static T GetControl<T>(this Control parent) where T : Control
    {
        return parent != null
                   ? parent.Controls.Cast<control>().Where(control => (control is T)).Cast<T>().FirstOrDefault()
                   : null;
    }

    public static T GetControl<T>(this Control parent, string name) where T : Control
    {
        return parent != null
                   ? parent.Controls.Cast<control>().Where(
                       control => (control is T) && (String.Equals(control.Name, name))).Cast<T>().FirstOrDefault()
                   : null;
    }
}

I think this code is cleaner and shorter.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s