Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a null value variable if it's a string?

Tags:

string

c#

null

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...

like image 423
Ohlin Avatar asked Jun 18 '13 06:06

Ohlin


People also ask

Can a string variable be null?

“A null String is Java is literally equal to a reserved word “null”. It means the String that does not point to any physical address.” In Java programming language, a “null” String is used to refer to nothing. It also indicates that the String variable is not actually tied to any memory location.

How do I check if a string is empty?

Java String isEmpty() Method The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How check string is null in JavaScript?

Use the === Operator to Check if the String Is Empty in JavaScript. We can use the strict equality operator ( === ) to check whether a string is empty or not. The comparison data==="" will only return true if the data type of the value is a string, and it is also empty; otherwise, return false .

Is string null or empty?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.


1 Answers

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!)

like image 178
Matthew Watson Avatar answered Oct 12 '22 23:10

Matthew Watson