What is the difference between:
int c; int c=new int();
Sometimes I use the first one and it works perfectly, but sometimes when I use inside a loop it doesn´t work.
this work:
public int[] junta(Table ent,int j)
{
int[] matriz=new int[count(ent)];
int k;
k = 0;
for (int i = fline(ent); i <= count(ent) + 1; i++)
{
if (Convert.ToString(ent.Cells[j, 3].Value) == Convert.ToString(ent.Cells[i, 3].Value))
{
matriz[k]=Convert.ToInt32(ent.Cells[i,0].Value);
k++;
}
}
}
this doesn´t work:
public int[] junta(Table ent,int j)
{
int[] matriz;
int k;
k = 0;
for (int i = fline(ent); i <= count(ent) + 1; i++)
{
if (Convert.ToString(ent.Cells[j, 3].Value) == Convert.ToString(ent.Cells[i, 3].Value))
{
matriz[k]=Convert.ToInt32(ent.Cells[i,0].Value);
k++;
}
}
}
The main difference is that this:
int c;
does not assign a value to the variable, so if you try to access the variable afterwards the compiler will complain with this:
Use of unassigned local variable 'c'
However if you do this:
int c = new int();
then you might as well write this:
int c = 0;
and this will assign a value to it.
You can test the following two code snippets and observe their difference:
int c;
int a = c;
with this:
int c = 0; // try = new int(); as well if you want to
int a = c;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments