C# 7.0 introduced pattern matching. This enables matches based on a specific class or structure. Since you can't change the signature of the method, this is the best option at the moment.
Code becomes simpler using the is expression to assign a variable if the test succeeds:
public void Place( object info )
{
if ( info is string infoString )
{
// use infoString here
}
else if ( info is int infoInt )
{
// use infoInt here
}
}
If statements are limited to test the input to match a specific single type. If you want to test specific cases, the switch pattern matching becomes a better choice:
public void Place( object info )
{
switch ( info )
{
// handle a specific case
case string infoString when infoString.Contains( "someInfo" ):
// do something
break;
case string infoString:
// do something
break;
case int infoInt when infoInt > 10:
// do something
break;
case int infoInt:
// do something
break;
default:
// handle any other case
break;
}
}
if(info is int)theiskeyword is made for this.