Is it possible to check if a variable is defined as a string if the value inside it is null?
If I write:
string b = null; bool c = b is string;
Then c will be false because looks at content that is null, not a string.
If I write:
string b = null; bool c = (b.GetType() == typeof(string));
Then it crashes because s is null and you cannot call GetType () at null.
So how can I check b to find out what type it is? Maybe some kind of reflection? Or is there an easier way?
Edit 1: Clarification of the question!
In my question, I was a bit unclear, and that was my mistake. In the example, it looks like I'm trying to check the contents of a variable. But I want to check the variable itself without looking at the content. In the above code examples, I can see that b is a string, but what if I don't know if b is a string and I just want to check the variable s to see if it is a string or not.
So how can I find out what type of variable is defined as? As in this example, but x is an unknown variable that can be defined as a string, and it can be null (since it can be null, this example will not work).
bool c = (x.GetType() == typeof(string));
Edit 2: Working Solution!
Thanks to all the answers, I was able to solve it. So the working solution began to work. First, I created a help function to check for a specific type of variable that works, even if the value is null and does not indicate anything.
public static Type GetParameterType<T>(T destination) { return typeof(T); }
Then I can just call this function and check my "suspicious string" and find out if it is really a string or not.
This is exactly what I wanted to know !!! Thank you all for squeezing your brains ...