8

I am using the function Array.Clear() to empty an array, but it generates an error. This is the code I was using:

private int[] activeFielderNumber = new int[10];
private string[] activeFielderAction = new string[10];  
....
...
....
Array.Clear(activeFielderNumber, 0, activeFielderNumber.Length);
Array.Clear(activeFielderAction, "", activeFielderAction.Length);

The error is:

error CS0103: The name `Array' does not exist in the current context

How can I solve this problem?

2
  • 2
    The error would indicate the class Array is not in scope. Could you expand your sample code to show at least the method declarations and maybe your usings? Commented Dec 7, 2012 at 5:28
  • There are multiple ways to clearing arrays. Even though Array.Clear() is the simplest in terms of code, if you're interested here are other methods Commented Jun 18, 2014 at 7:55

8 Answers 8

11

did you use

using System;

and one more point to correct:

Array.Clear(activeFielderAction, "", activeFielderAction.Length);

it should be

Array.Clear(activeFielderAction, 0, activeFielderAction.Length);

The last two parameters are the index-ranges to be cleared.

Sign up to request clarification or add additional context in comments.

Comments

7

Try this

Array.Clear(yourArray, 0, yourArray.Length);

Comments

3

I am using Array.Clear() function to empty array. But it was throwing error

No, i was not. The Clear function was not throwing an error, the COMPILER was.

error CS0103: The name `Array' does not exist in the current context

Google says the following when we look for CS0103:

Compiler Error CS0103 (C#) at MSDN

An attempt was made to use a name that does not exist in the class, namespace, or scope. Check the spelling of the name and check your using statements and assembly references to make sure that the name you are trying to use is available. One common mistake is to declare a variable within a loop or a try block and then attempt to access it from an enclosing code block or another code block, as shown in the following example.

Translates into: Array is not found in the context. Are you missing a "using" statement?

Comments

1

The following code worked:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] activeFielderNumber = new int[10];
string[] activeFielderAction = new string[10];  

Array.Clear(activeFielderNumber, 0, activeFielderNumber.Length);
Array.Clear(activeFielderAction, 0, activeFielderAction.Length);
        }
    }
}

Comments

1

Array.Clear() method only resets the array to its default state.

Based on the statement

Array.Clear(activeFielderAction, "", activeFielderAction.Length); we may get an error.

The actual statement should be

Array.Clear(activeFielderAction, 0, activeFielderAction.Length);

Also check whether you are importing Using.System; namespace.

Try the below code.

int[] activeFielderNumber = new int[10];
activeFielderNumber[1] = 10;
activeFielderNumber[2] = 20;

string[] activeFielderAction = new string[10];
Array.Clear(activeFielderNumber, 0, activeFielderNumber.Length);
Array.Clear(activeFielderAction, 0, activeFielderAction.Length);

Comments

1

Unfortunately, I don't have enough points to post a comment, so have to provide an "answer" here... for any "speed junkies" out there, there are numerous ways to clear an array (not just Array.Clear) as demonstrated here, but typically Array.Clear is the easiest and fastest.

Here are 3 ways that were tested to clear an array (direct from the site) where "o1" is the object array:

for (int x = 0; x < MAX; x++)
{
    o1[x] = null;
}

Array.Clear(o1, 0, o1.Length);

Parallel.For(0, MAX, x =>
{   //arrays are thread safe if only one thread is writing to an index
    o1[x] = null;
});

I thought it was interesting reading as I never gave a second thought to trying another way (especially using a parallel loop) other than Array.Clear though.

Comments

0

The line

Array.Clear(activeFielderAction, "", activeFielderAction.Length);

is not a valid use of Array.Clear() - the middle parameter needs to be an int, like you have on the previous line.

Comments

0

You just simply set Array object reference to null example

String url = "http://localhost/RestWebService/employee?" name + "," + id; String[] arr = url.Split('?');

        arr = null;
        arr = url.Split('?');

Thats all

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.