This code is working.
public class Hello{
public static void Main(){
int a = 1;
int[] i = new int[]{1,2,3};
foreach(int n in i)
{
a = n;
}
System.Console.WriteLine(a);
}
}
This code is good too.
public class Hello{
public static void Main(){
int a ;
int[] i = new int[]{1,2,3};
System.Console.WriteLine(a);
}
}
Although, next code is not working. Compiler says "Use of unassigned local variable `a'".
public class Hello{
public static void Main(){
int a ;
int[] i = new int[]{1,2,3};
foreach(int n in i)
{
a = n;
}
System.Console.WriteLine(a);
}
}
Why is this code bad?