6

Is it possible to test 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 is looks at the content, which is null and not a string.

If I write:

string b = null;
bool c = (b.GetType() == typeof(string)); 

Then it crashes because s is null and you can't call GetType() on a null value.

So, how can I check b to find out what type it is? Some kind of reflection maybe? Or is there any simpler way?

Edit 1: Clarification of the question!

I was a bit unclear in my question and that was my fault. In the example it looks like I'm trying to test the content of the variable. But I want to test the variable itself without looking at the content. In the code examples given I can see that b is a string, but what if I don't know if b is a string and just want to test the variable s to see if it is a string or not.

So, how can I know what type the variable is defined as? As in this example, but x is an unknown variable that might be defined as a string and it might be null as well (since it might be null this example won't work).

bool c = (x.GetType() == typeof(string)); 

Edit 2: The working solution!

Thanks to all the answers given I was able to solve it. This is how the working solution became. I first created a help function to test the defined type of a variable that works even if the value is null and it doesn't point to anything.

public static Type GetParameterType<T>(T destination)
{
    return typeof(T);
}

Then I can just call this function and test my "suspected string" and find out if it really is a string or not.

// We define s as string just for this examples sake but in my "definition" we wouldn't be sure about whether s is a string or not.
string s = null; 

// Now we want to test to see if s is a string
Type t = GetParameterType(s);
b = t == typeof(string);  // Returns TRUE because s has the type of a string
b = t is string;  // Returns FALSE because the content isn't a string

This is just what I wanted to find out!!! Thank you all for squeezing your brains...

Ohlin
  • 4,068
  • 2
  • 29
  • 35
  • 7
    possible duplicate of [.NET : How do you get the Type of a null object?](http://stackoverflow.com/questions/254461/net-how-do-you-get-the-type-of-a-null-object), [C# get type of null object](http://stackoverflow.com/q/930147), [Getting the underlying type of a null object](http://stackoverflow.com/q/3650060), [Get type of null reference object for Object reference not set to an instance of an object](http://stackoverflow.com/q/3663802) – Cody Gray - on strike Jun 18 '13 at 06:13
  • 1
    I doubt if you can do that coz null is not symbolic to a specific type – V4Vendetta Jun 18 '13 at 06:17
  • You are probably trying to solve a problem that would be better solved in a different way. Can you elaborate on what you are trying to do? – Sean Jun 18 '13 at 06:22
  • This problem is mainly out of curiosity. I don't like it when I don't know how to do a certain thing and then I just want to find out. If it's not possible to fix then that's fine, I just want to know. I hope my edit of the question can bring some more clarity of how I was thinking. – Ohlin Jun 18 '13 at 06:29
  • What makes you to know Type of object you don't know what type of it is? – Imran Rizvi Jun 18 '13 at 06:36
  • @Ohlin: Any chance we would move that to the answer that is marked as an answer? That is exactly what he suggests, and answering in the question is generally a bad idea. :) I'm not sure this actually solves your problem though. Consider this: `string s = null; object o = s; Type type = GetParameterType(o); bool isString = typeof(string) == type; /* this evaluates to false */` Again, you might be heading down a wrong path when it comes to understanding your own problem. – Johny Skovdal Jun 18 '13 at 06:55
  • @Johny I know my question was a bit theoretical and maybe doesn't have that much real life value, but it was interesting to me. In your example o is defined as an object and that's also what you're testing. When you assign o = s you just transfer the null value. I was never interested in the value but in the original declaration of the variable and o was never declared as a string. As I said, a bit theoretical but it was bothering me because I couldn't figure out how to do it. The answer I accepted below was the answer that linked me to a working solution. I just clarified it in my question. – Ohlin Jun 18 '13 at 07:00

6 Answers6

11

You cannot check the type of null because null has no type. It doesn't reference anything at all, therefore there is nothing that C# can look at to find out the actual type.

(Everyone else seems to be answering the question "How can I tell if a string reference is null or empty - but I think the question is "how can I tell if the underlying type of a null reference is string...)

There might be a way to fiddle it though - you might be able to use a generic method as mentioned here:

.NET : How do you get the Type of a null object?

(That link was posted by someone else - not me - as a comment to your original post!)

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Yes, you're the only one that got my question :-) But is there no way to check how the variable was defined even though it might have a null value? – Ohlin Jun 18 '13 at 06:26
  • @Ohlin - Did you look at any of the duplicate links in the first comment under your question? – Tim Jun 18 '13 at 06:27
  • @Ohlin I've copied into my answer the link that someone else posted as a comment under your original question. It seems to give a possible answer. – Matthew Watson Jun 18 '13 at 06:30
  • "How do I check the compile-time type of my variable?" Just look at the code. Where the variable is declared you can see that type. The IDE will help you if you hover the mouse of the variable's name, it will show the compile-time type. If you need a `System.Type` object representing the corresponding runtime type, write `typeof(KnownClass)`. No real need for a generic method for this, in my opinion. (Anonymous types are a special case.) "How do I get the actual runtime type of the instance?" Since the variable holds a null reference, there is no instance, so you can't; it makes no sense. – Jeppe Stig Nielsen Jun 18 '13 at 06:40
  • @MatthewWatson Thanks for the link. Yes, that link was the reason of how I could come up with a solution. To simply write `typeof(s)` doesn't work, probably because string is a type and not object. But when put inside a generic function it does work! C# probably makes some kind of conversion of my `s` variable so I can use the typeof function. Thanks a lot! – Ohlin Jun 18 '13 at 06:50
3

So you want to know if there is a direct method to check of an object type whose value is set to NULL

In The simple word the answer is NO.

A null reference does not point to any Storage Location, so there is no metadata from which it can make that determination.

Although if you already know it is of type String , you can use following two functions for checking null values

String.IsNullOrWhiteSpace(stringObject);

and

String.IsNullOrEmpty(stringObject)

The best that you could do to set a value to unknown type is use

Convert.ChangeType 

e.g. as given in .NET : How do you get the Type of a null object?

public void GetObjectValue<T>(out T destination)
{
    object paramVal = "Blah.Blah.";
    destination = default(T);
    destination = Convert.ChangeType(paramVal, typeof(T).GetType());
}

The type of T can be inferred, so you shouldn't need to give a type parameter to the method explicitly.

Community
  • 1
  • 1
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
1

Here it is

String.IsNullOrEmpty(s);
Paritosh
  • 11,144
  • 5
  • 56
  • 74
1

If you want to tell whether the actual value is a string, you can't do that with null, as null doesn't have a type.

So it seems you want to determine the actual type of the variable (the one it was declared as):

If the variable is of type string, then you know it at compile time (you declared it as string, after all).
If the variable is generic (like in generic types or a generic method), you can test it via typeof(T), assuming T is your type parameter.
If you got the variable as object though (e.g. as argument of a method), then there is no way to determine its original type if its value is null.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0
  1. IsNullOrWhiteSpace(variableName)

  2. string.IsNullOrEmpty()

  3. var.Trim().Length < 1

The third one is the one I personally use, as it is version-independent.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Zia
  • 921
  • 5
  • 14
  • 3
    I wasn't the downvoter, but the reason is probably because you didn't answer the question: **how can I check b to find out what type it is?** Lots of answers here seem to be focused on the title of the post (which is misleading). – Tim Jun 18 '13 at 06:17
  • @tim ok , let the moderator edit the question accordingly. then i will remove my answer – Zia Jun 18 '13 at 06:20
0

Use the String.IsNullOrEmpty method to check it.

string b = null;
bool c = String.IsNullOrEmpty(b);

See this MSDN link for further details.

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56