-2

Update : my question wasn't correctly formulated

When I want to test a variable with String.IsNullOrEmpty, Visual Studio complains it is unassigned whereas I did assign it see code below ?

String IniFileExtension;


if (File.Exists(Path.Combine(fileDirectory, iniFileName)))
{

    // read ini file
    IniFile iniFile = new IniFile(Path.Combine(fileDirectory, iniFileName));
    string outputFileName= iniFile.IniReadValue("Params","outputFile");
    IniFileExtension= iniFile.IniReadValue("Params","fileExtension");
}

if (String.IsNullOrEmpty(MyString)) {}
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 3
    Why not just initialize it with null and then test if it is still null after you assign the path to the variable? – thewisegod Sep 27 '15 at 00:06
  • More importantly, the above code won't even compile. – rbm Sep 27 '15 at 00:10
  • 1
    @rbm The OP is asking why "Visual Studio complains" [with a compiler error and doesn't compile]. – user2864740 Sep 27 '15 at 00:10
  • Agreed, what i meant to say is that compiler error "Use of unassigned local variable" is pretty clear; i.e. there should not be a way do to so. – rbm Sep 27 '15 at 00:13
  • 3
    I think what you're missing is the fact that *null* is different to *unassigned*. – Blorgbeard Sep 27 '15 at 00:28
  • possible duplicate of [Why doesn't C# default to null for unassigned local variables?](http://stackoverflow.com/questions/14470277/why-doesnt-c-sharp-default-to-null-for-unassigned-local-variables) – M.kazem Akhgary Sep 27 '15 at 04:16
  • The IsNullOrEmpty is just like other methods in. Net. No special treatment. – M.kazem Akhgary Sep 27 '15 at 04:17

2 Answers2

3

The error is because a value has not been definitely assigned to the local variable; and thus the local variable cannot be used in any expression1.

It has naught to do with IsNullOrEmpty and can be reproduced in a number of forms:

string x;
string y = "Hello " + x; // <- invalid, x is not definitely assigned.
bool z = null == x;      // <- invalid, x is not definitely assigned.
"Hello".Equals(x);       // <- invalid, x is not definitely assigned.

See Are C# uninitalized variables dangerous? for details about why this is required. Most of the related questions are about a specific form of assignment analysis; but here it was trivially never assigned.

As such - because the code doesn't compile - it is a 'useless' construct as shown.


1 Digression: it can still be used as an out parameter.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • @user310291 It will still fail to compile for the same reason. The code added still fails to assign (much less definitely assign) a value to the MyString variable. Even if it was assigned in the 'if' it would still not be definitely assigned - e.g. condition may have been false. The compiler must be able to prove the assignment before use, when runtime information is not available. (The rules for definite assignment are simplistic local branch analysis.) – user2864740 Sep 27 '15 at 07:46
  • @user310291 The question link I added to the answer has many related questions to specific analysis cases relating to definite assignment. – user2864740 Sep 27 '15 at 07:49
  • user2864740 I am surprised that the compiler would analyze the if. But OK thanks. – user310291 Sep 27 '15 at 08:16
1

No, the method is very much correct and is working as expected.

It can be the case that your variable is assigned a value and in the due course it becomes null.

For e.g.

String MyString = "Some String";

//In due course MyString becomes null

if (String.IsNullOrEmpty(MyString)) {}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208