How to clear an array in C #?

I use the Array.Clear() function to delete an array, but it generates an error. This is the code I used:

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

Mistake:

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

How can I solve this problem?

+7
source share
8 answers

did you use

 using System; 

and one more point to fix:

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

he should be

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

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

+9
source

I am using the Array.Clear () function for an empty array. But it caused an error

No. I was not. The Clear function did not give an error, COMPILER was.

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

Google says the following when we search for CS0103:

Compiler Error CS0103 (C #) on MSDN

An attempt was made to use a name that does not exist in the class, namespace or scope. Verify that the name is spelled correctly and use the assembly instructions and links to verify that you are trying to use the name. One common mistake is to declare a variable in a loop or try block, and then try to access it from the closing code block or other code block, as shown in the following example.

Translated to: Array not found in context. Are you missing the "using" statement?

+5
source

try it

 Array.Clear(yourArray, 0, yourArray.Length); 
+4
source

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); } } } 
+2
source

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

Based on statement

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

The actual statement should be

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

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

Try the code below.

 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); 
+1
source

Unfortunately, I don’t have enough points to post a comment, so you need to provide an “answer” here ... for any “high-speed addicts”, there are many ways to clean the array (not just Array.Clear ) as shown here , but usually Array.Clear is the easiest and fastest.

Here are three ways to check the array ( directly from the site ), where "o1" is an array of objects:

 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 this was interesting since I never thought of any other way (especially using a parallel loop) other than Array.Clear though.

+1
source

Line

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

is not a valid use of Array.Clear () - the middle parameter should be int, as on the previous line.

0
source

It’s just that you simply set the reference to the Array object to a null example

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

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

Thats all

0
source

All Articles