foreach (var item in mainCanvas.Children)
{
if (item is Button)
{
(item as Button).Content = "this is a button";
}
}
Can I use LINQ or other feature of .NET 4 to be more concise (maybe performant)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
Enumerable.OfType:Performance Measurements
Method 1: OPs original suggestion
Method 2: OfType
Method 3: Only cast once
Method 4: for loop:
Results
Conclusion
forloop instead offoreach.OfTypeis considerably slower.But remember to optimize readability first, and only optimize performance if you have performance profiled and found that this specific code is the performance bottleneck.