Testing fro null or empty string

I know this is a basic C# question but I cannot remember the answer.
If I want to test for the string condition before processing can I use
var strTest = null;
if(strTest == null || strTest.trim() == string.Empty)

Of do I have to break them up like
if(strTest == null)
if(strTest.trim() != string.Empty)
   Do something here......

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
Perfect!  Thank you.
As p_davis said:
using System;

namespace EE_Q28702578
	class Program
		static string s1 = default(string);	// Null string
		static string s2 = "";				// Empty string
		static string s3 = " ";				// Whitespace string - technically not empty
		static string s4 = "Unempty string";	// Self explanatory

		static void Main(string[] args)
			if (s1.IsNullOrWhiteSpace())
				Console.WriteLine("s1 is a null or whitespace string.");
				Console.WriteLine("s1 is not a null or whitespace string.");

			if (s2.IsNullOrWhiteSpace())
				Console.WriteLine("s2 is a null or whitespace string.");
				Console.WriteLine("s2 is not a null or whitespace string.");

			if (s3.IsNullOrWhiteSpace())
				Console.WriteLine("s3 is a null or whitespace string.");
				Console.WriteLine("s3 is not a null or whitespace string.");

			if (s4.IsNullOrWhiteSpace())
				Console.WriteLine("s4 is a null or whitespace string.");
				Console.WriteLine("s4 is not a null or whitespace string.");


	static class Extensions
		// We have to implement this prior to .NET 4...
		public static bool IsNullOrWhiteSpace(this string source)
			return string.IsNullOrEmpty(source) || source.Trim().Length == 0;

Open in new window

Which produces the following output -Capture.JPG***NOTE***
IsNullOrWhiteSpace is native in .NET 4 and above, it has to be implemented in .NET 3.5 and below.

lapuccaAuthor Commented:
Thank you Saige,
I forgot to mention I'm using .net 4.5 so it works for me.  Thank you for the reply.  I'm sorry that I already give points away but I will mark yours as good comment.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.