ToolTip ToolTip toolTip

While working on a WPF control that handles some aspects of its (optional) tooltip programmatically, I wrote the following lines:

if (ToolTip is ToolTip toolTip)
{
  // … do something with toolTip …
}

This makes perfect sense and isn’t confusing inside Visual Studio (because of syntax highlighting), but made me smile nevertheless.


Explanation:

  • The code uses the “is” shorthand introduced in C# 7 that combines testing for “not null” and the desired type with declaring a corresponding variable (I love it!).
  • The first “ToolTip” is the control’s ToolTip property (FrameworkElement.ToolTip), which is of type “object”.
  • The second “ToolTip” is the class “System.Windows.Control.ToolTip”.

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website