Anonymous wrote:
> Which is the recommended way in C# to test if an object is of type string?
>
> In VB.NET I wrote "If TypeOf obj Is String Then".
> That meets the requirements:
> 1. You don't have to instance an object.
> 2. Doesn't throw exception if obj is null.
>
> I havn't found any solution to that in C#. Does it exist any simple syntax?
You can either use the is or as operator, depending on if you later have
use for a reference to the string:
if (obj is string) {
...
}
or
string s = obj as string;
if (s != null) {
...
}
--
Göran Andersson
_____
http://www.guffa.com