Dotnet выдаёт ошибку: error CS0165: Использование локальной переменной "code", которой не присвоено значение.
код:
using System.IO;
ReadFile();
void ReadFile()
{
String code;
try
{
Console.Write("Enter txt-file path: ");
string path = Console.ReadLine();
StreamReader sr = new StreamReader(path);
code = sr.ReadLine();
while (code != null)
{
Console.WriteLine(code);
code = sr.ReadLine();
}
sr.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
string code1 = code;
string[] words = code1.Split('\n');
Console.WriteLine(words[2]);
}
так уже пробовал:
using System.IO;
ReadFile();
void ReadFile()
{
String code = "";
try
{
Console.Write("Enter txt-file path: ");
string path = Console.ReadLine();
StreamReader sr = new StreamReader(path);
code = sr.ReadLine();
while (code != null)
{
Console.WriteLine(code);
code = sr.ReadLine();
}
sr.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
string code1 = code;
string[] words = code1.Split('\n');
Console.WriteLine(words[2]);
}
Как можно исправить? Помогите!
string code1 = code;
...