如何清空 C# 中的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13757390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:42:14  来源:igfitidea点击:

How to empty the Array in C#?

c#arrays

提问by Ethan

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

我正在使用该函数Array.Clear()来清空一个数组,但它会产生一个错误。这是我使用的代码:

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?

我怎么解决这个问题?

回答by user366312

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

回答by Azodious

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.

最后两个参数是要清除的索引范围。

回答by TomTom

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

我正在使用 Array.Clear() 函数来清空数组。但它抛出错误

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

不,我不是。Clear 函数没有抛出错误,编译器是。

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. 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.

尝试使用在类、命名空间或范围中不存在的名称。检查名称的拼写并检查您的 using 语句和程序集引用,以确保您尝试使用的名称可用。一个常见的错误是在循环或 try 块中声明一个变量,然后尝试从封闭的代码块或另一个代码块访问它,如以下示例所示。

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

转换为:在上下文中找不到数组。您是否缺少“使用”语句?

回答by slugster

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,就像你在上一行中所做的那样。

回答by Vamsi Vithala

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.

Array.Clear(activeFielderAction, "", activeFielderAction.Length);我们可能会得到一个错误。

The actual statement should be

实际的说法应该是

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

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

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

还要检查你是否是importing Using.System;命名空间。

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

回答by Vamsi Vithala

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.Clearis 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.Clearthough.

我认为这是有趣的阅读,因为我从来没有想过尝试除Array.Clear之外的另一种方式(尤其是使用并行循环)。

回答by Manoj

Try this

尝试这个

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

回答by user3728743

You just simply set Array object reference to null example

您只需将 Array 对象引用设置为 null 示例

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

字符串 url = " http://localhost/RestWebService/employee?" 名称 + "," + id; String[] arr = url.Split('?');

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

Thats all

就这样