How to check a null variable if it is a string?

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.

 // 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 exactly what I wanted to know !!! Thank you all for squeezing your brains ...

+6
source share
6 answers

You cannot check for null type because null has no type. It does not reference anything, so there is nothing that C # can look at to find out the actual type.

(It seems that everyone else answers the question "How can I determine if a string is a link empty or empty?", But I think the question is "how can I determine if a base type is a null reference string ...)

Maybe there is a way to script it - maybe you can use the general method mentioned here:

.NET: How do you get a null object type?

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

+11
source

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

In a simple word, the answer is NO.

A null reference does not indicate any storage location, so there is no metadata from which it can make this determination.

Although, if you already know that it is a String type, you can use the following two functions to check for null values

 String.IsNullOrWhiteSpace(stringObject); 

and

 String.IsNullOrEmpty(stringObject) 

The best you could do to set the value to an unknown type is to use

 Convert.ChangeType 

eg. as stated in .NET: How do you get a null object type?

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

Type T can be inferred, so you do not need to explicitly specify a type parameter.

+3
source

Here

 String.IsNullOrEmpty(s); 
+1
source

If you want to know if the actual value is a string , you cannot do this with null , since null not of type.

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

If the variable is of type string , then you know it at compile time (in the end, you declared it as string ).
If the variable is generic (for example, in generic types or a generic method), you can test it with typeof(T) , assuming that T is your type parameter.
If you get the variable as object though (for example, as a method argument), then there is no way to determine its original type if its value is null .

+1
source
  • IsNullOrWhiteSpace(variableName)

  • string.IsNullOrEmpty()

  • var.Trim().Length < 1

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

0
source

Use the String.IsNullOrEmpty method to test it.

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

See the MSDN link for details.

0
source

Source: https://habr.com/ru/post/947522/


All Articles