1

hi this is my code i want to add string in list but i get this error

use of unassigned local variable

and this is my code

List<string> tokens = new List<string>();
int token_number=0 ;
char[] character = file_text.ToCharArray();
int i = 0;
for (; i < character.Length; i++)
{
    if (char.IsLetter(character[i]))
    {
        string local_token;
        while (char.IsLetterOrDigit(character[i + 1]) || character[i + 1] == '_')
        {

            local_token += character[i];
            tokens.Add(local_token.ToString());
        }

    }
}

hi this is my code and i want to add string to my list so get this error Error 2 Use of unassigned local variable 'local_token' C:\Users\Alireza\Documents\Visual Studio 2013\Projects\tslnc\tslnc\Program.cs 46 25 tslnc

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Alireza
  • 2,744
  • 2
  • 17
  • 21
  • Your string `local_token` is not an empty string but `null` when you don't initialize it. Then you can't use it and `local_token += character[i]` will fail with a `NullReferenceException` at runtime anyway. That's what the compiler wants to tell you. You just have to assign a value like `""`. ` – Tim Schmelter Nov 20 '15 at 11:26
  • Apart from the answers below, also note that you should probably be adding `local_token` to `tokens` OUTSIDE the `while` loop. – Matthew Watson Nov 20 '15 at 11:27
  • 1
    Would it not be worth using a `StringBuilder` for this rather than appending using `+`? – Chawin Nov 20 '15 at 11:32
  • [Why compile error “Use of unassigned local variable”?](http://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable) – Reza Aghaei Nov 20 '15 at 11:33
  • [Fields (C# Programming Guide)](https://msdn.microsoft.com/en-us/library/ms173118.aspx) – Reza Aghaei Nov 20 '15 at 11:34

5 Answers5

4

Change your declaration of the local_token to this:

string local_token = "";

If you don't specify a value for the string it isn't initialized (unassigned).

Ryan Searle
  • 1,597
  • 1
  • 19
  • 30
3

You declare a variable:

string local_token;

Then you try to reference its value:

local_token += character[i];

But you never gave it a value. It's null. It's unassigned.

Simply declare it with a default value:

string local_token = string.Empty;
David
  • 208,112
  • 36
  • 198
  • 279
1

I am sharing very simple one liner answer:-

You are only generating new instance, but you didn't assigned with any value, So you are getting run time error.

string local_token;

local_token = string.empty;

Now just add 2nd line also in your code. Hope this post will make more help :).

Chetan Sharma
  • 334
  • 2
  • 11
0

you have to intialize variable before use...

 string local_token=string.Empty;
Viru
  • 2,228
  • 2
  • 17
  • 28
0

To expand on others answers, rather than using a string for local_token you could use a Stringbuilder.

In a loop, a Stringbuilder will be a lot faster and use a lot less memory than appending to a string using + ("How to improve string concatenation performance in Visual C#").

To implement the Stringbuilder:

   StringBuilder local_token = new Stringbuilder();
    while (char.IsLetterOrDigit(character[i + 1]) || character[i + 1] == '_')
    {
        local_token.Append(character[i]);
        tokens.Add(local_token.ToString());
        local_token.Clear();
    }
Chawin
  • 1,438
  • 1
  • 21
  • 33