0

In Microsoft Visual Studio (Community Edition of course) I have a simple C# program where I declare a simple array without a set size like so

int[] paths;

In a later part of the program I try to set parts of the array but it gives me an error

Use of unsigned local variable 'paths'

in this part of the program

for (int b = 0; b < 100; b++) { 
      paths[b] = b;
}

In conclusion, I suppose I'm wondering whether this problem that affects just me, or a problem of syntax. (I originated from Java which is half to blame how I ran into this question) I am willing to accept closure on whether this is possible or not,(If so source please) or If I'm just doing it wrong.(If so please provide a solution/suggestions in comments)

1
  • 1
    Arrays in C# are not dynamic in size. You have to declare its size. int[] paths = new int[100] Commented Aug 19, 2015 at 6:01

5 Answers 5

3

In C# Arrays are fixed size, and need to be initialized

var size = 100;
int[] paths = new int[size];
for (var b = 0; b < size; b++) { 
      paths[b] = b;
}

List is also another good thing for a C# beginner to know about, the difference between an array and a list, is that a List will resize itself for you automagically (I believe there is a very similar List class in Java)

var size = 100;
var paths = new List<int>();
for (var b = 0; b < size; b++) { 
      paths.Add(b);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You just declared the array, but you also need to create it before using it:

paths = new int[100];

Comments

0

You must follow the array declaration syntax.

int[] paths; //-> here you are declaring the paths array and your not creating array.

in memory it set paths array without its sizes. So you must set the size by creating this array as follows.

int[] paths = new int[10];

you can create and declare array like as follows, for to assign another array with it. EG:

int[] arr1 = new int[] { 0,1,2,3,4,5,6,7};
int[] paths =arr1;

here arr1 size and data will be automatically assigned to paths array.

Comments

0

Firstly you are creating a variable but you have not assigned it at all, this is the reason for you error.

Secondly in C# arrays require a fixed size and can not be resized so when initializing a size must be provided, either explicitly as below or implicitly by initializing it with objects (see comments).

int[] paths; // declaration
paths = new int[100] // assignment, array initialized with a size of 100
// with objects
// paths  = new int[] { 1, 2, 3 }; 

// declaration and assignment can also be merged into 1 line.
// i.e int[] paths = new int[100]

for (int b = 0; b < 100; b++) { 
      paths[b] = b;
}

Comments

0

you must declare and initialize the array element in your code like

int[] myInt = new int[100]

If you don't know the size before initialize you need to use List like

List<int> list = new List<int>();

list.Add(123);
list.Add(456);
list.Add(789);

Console.WriteLine(list[2]);

and it is necessary to use array and size of array is not fixed before initialize you need to follow below code but below code is wastage of memory usage and more execution process. so my recommend is use List instead of int[]

// Global Variable
int[] myInt;
int mySize = 1;

private void button1_Click(object sender, EventArgs e)
{
    if (mySize > 1)
    {
        int[] tempInt = new int[mySize];
        tempInt = myInt;
        myInt = new int[mySize];
        for (int i = 0; i < mySize - 1; i++)
        {
            myInt[i] = tempInt[i];
        }
        myInt[mySize - 1] = mySize;
    }
    else
    {
        myInt = new int[mySize];
        myInt[mySize - 1] = mySize; 
    }
    mySize++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.