简体   繁体   English

如何在C#中清空数组?

[英]How to empty the Array in C#?

I am using the function Array.Clear() to empty an array, but it generates an error. 我使用函数Array.Clear()来清空数组,但它会生成错误。 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? 我怎么解决这个问题?

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. 最后两个参数是要清除的索引范围。

试试这个

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

I am using Array.Clear() function to empty array. 我正在使用Array.Clear()函数来清空数组。 But it was throwing error 但这是投掷错误

No, i was not. 不,我不是。 The Clear function was not throwing an error, the COMPILER was. 清除功能并没有抛出错误,COMPILER就是。

error CS0103: The name `Array' does not exist in the current context 错误CS0103:当前上下文中不存在名称“Array”

Google says the following when we look for CS0103: 当我们寻找CS0103时Google会说以下内容:

Compiler Error CS0103 (C#) at MSDN MSDN上的编译器错误CS0103(C#)

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. 检查名称的拼写并检查using语句和程序集引用,以确保您尝试使用的名称可用。 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. 一个常见的错误是在循环或try块中声明变量,然后尝试从封闭的代码块或其他代码块访问它,如以下示例所示。

Translates into: Array is not found in the context. 转换为:在上下文中找不到数组。 Are you missing a "using" statement? 你错过了“使用”声明吗?

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);
        }
    }
}

Array.Clear() method only resets the array to its default state. Array.Clear()方法仅将数组重置为其默认状态。

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; 还要检查是否要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);

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. 不幸的是,我没有足够的分数来发表评论,所以必须在这里提供一个“答案”...对于那里的任何“速度迷”,有很多方法可以清除数组(不仅仅是Array.Clear如此处所示 ,但通常Array.Clear是最简单和最快的。

Here are 3 ways that were tested to clear an array ( direct from the site ) where "o1" is the object array: 以下是3种方法,用于清除数组( 直接来自网站 ),其中“o1”是对象数组:

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. 我认为这是有趣的阅读,因为我从未想过尝试另一种方式(特别是使用并行循环)而不是Array.Clear

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. 不是Array.Clear()的有效用法 - 中间参数需要是一个int,就像你在前一行一样。

You just simply set Array object reference to null example 您只需将Array对象引用设置为null示例即可

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

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

Thats all 就这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM