IF condition with a string
Just a quick 开发者_StackOverflow社区one:
string firstline;
if (firstline == null) {
System.Console.WriteLine("String empty!");
}
In theory if there is no value in "firstline", the console should out put "String empty!"?
This doesn't even compile because of:
Use of unassigned local variable 'firstline'
When you say you don't get any output but the program compiles and runs just fine, you are not showing us your real code.
However, if firstline
is not a local variable but a member variable of the surrounding class, it will be automatically initialized with null
. In general, all member variables of a class are initialized with default(T)
where T
is the type of the member variable.
You can't compile in VS with error: Use of unassigned local variable 'firstline' !
Try assign null before!
EDIT
or
class Program
{
static string firstline; # with static you can compile and got your behaviour
static void Main(string[] args)
{
if (firstline == null)
{
System.Console.WriteLine("String empty!");
}
Console.ReadKey();
}
}
In default they are not null. If you want it null by default use this:
static string firstline;
static void Main(string[] args)
{
if (firstline == null)
{
System.Console.WriteLine("String empty!");
}
}
But I suggest using this one:
static void Main(string[] args)
{
string firstline = null;
// or this:
//string firstline = String.Empty;
if (String.IsNullOrEmpty(firstline))
{
System.Console.WriteLine("String empty!");
}
}
In both ways you can get riddle of
Use of unassigned local variable 'firstline'
Yes that will behave as you expect and should print out to the console. You might find that the console application closes too quickly for you to read the result though if you've not got any following code.
Also null and "" (or String.Empty) often mean the same thing, so a more command way to do this is:
if(String.IsNullOrEmpty(firstline))
{
Console.WriteLine("String is empty!");
}
Your terminology makes it sound like you are a little confused.
A null
string is precisely that - the absence of the value. It is not an empty string, it is a string with no value.
An empty string is a zero length string, ""
or string.Empty
. This is not null
as it does have a value, but the value is zero length.
Often you want to treat the null and empty values the same, in which case you might use the check
if (string.IsNullOrEmpty(firstline))
{
System.Console.WriteLine("String is null or empty!");
}
Your code is not correct, Use of unassigned local variable 'firstline'. You can assign it with any value to test. If you want to check if it is an empty string, a better way is:
string firstline = null; //change to "" to test
if (string.IsNullOrEmpty(firstline))
{
System.Console.WriteLine("String empty!");
}
here are my 2 cents:
if (firstline == (string)null) throw new ArgumentNullException("firstline"); //value is null
if (firstline.Length == 0) throw new ArgumentOutOfRangeException("firstline", "Value is empty"); // string.Empty
I found this by using Pex and Moles
精彩评论