静态数组的定义

发布于 2024-10-10 10:29:14 字数 98 浏览 7 评论 0原文

我一直在寻找静态数组的合适定义。我尝试过使用 msdn 和 c# 源代码,但似乎找不到定义。它给出了示例,但没有定义...

有人知道静态数组的任何链接或定义和特征吗?

I've been looking for a decent definition of static arrays. I've tried using msdn and c# sources but cannot seem to find a definition. It gives examples, but no definitions...

Does anyone know any links or definitions and characteristics of a static array please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

給妳壹絲溫柔 2024-10-17 10:29:14

当您谈论“静态数组”时,您实际上是在谈论两个不同的事物。

其中之一是static 关键字。当应用于变量时,这意味着该变量位于类级别,并且该类型的每个对象都不会获得自己的实例。

数组只是一种用于保存某种类型的多个值的数据结构。

因此,静态数组只是类级别的数组,可以保存多种数据类型。

例如:

在您的 TravelRoute 类中,您的路线中可能有一定数量的可能目的地。这些可以这样定义:

class TravelRoute {
    public static Destination[] possibleDestinations = 
           new Destination[]{
                 new Destination("New York"),
                 new Destination("Minneapolis"),
                 new Destination("NParis")
           };
}

这将定义 TravelRoute 上可能的目的地。然后您可以像这样访问该数组:

Destination one = TravelRoute.possibleDestinations[0];

When you talk about a 'static array' you are really talking about two separate things.

One would be the static keyword. When applied to variables this means that the variable lives at the class level, and each object of that type will not get its own instance.

An array is simply a data structure for holding multiple values of some type.

So, a static array is simply an array at the class level that can hold multiple of some data type.

For example:

In your TravelRoute class, you may have a set number of possible destinations in a route. These could be defined like so:

class TravelRoute {
    public static Destination[] possibleDestinations = 
           new Destination[]{
                 new Destination("New York"),
                 new Destination("Minneapolis"),
                 new Destination("NParis")
           };
}

This will define the possible destinations on a TravelRoute. You can then access the array like so:

Destination one = TravelRoute.possibleDestinations[0];
天邊彩虹 2024-10-17 10:29:14

您可能指的是固定大小的数组吗?

unsafe struct Foo
{
    fixed int Values[8];
}

如果是这样,通过使用固定大小的数组作为查询,您将获得更多搜索结果:)

Do you possibly mean fixed size arrays?

unsafe struct Foo
{
    fixed int Values[8];
}

If so, you will get more search results by using fixed size arrays as your query :)

美人如玉 2024-10-17 10:29:14

(据我所知)静态数组本身没有什么特别的,这可能就是为什么你很难找到关于它们的好的文章。如果我错了,请纠正我,但我想知道您最感兴趣的是“静态”部分吗?基本上,静态意味着成员存在于类级别而不是实例级别,因此静态数组将是属于该类(而不是该类的实例)的数组。

例子:

public class Foo
{
   public static int[] Numbers {get; set;}
}

public class Bar
{
   public int[] Numbers {get;set;}
}

public class Program
{
     public void Main()
     {
// NOTE: Foo itself has this array
          Foo.Numbers = new int[]{1,2,3};

// NOTE: it's this particular instance of a Bar that has this array
           Bar bar = new Bar();
           bar.Numbers = new int[]{1,2,3};

     }
}

There's nothing (that I know of) that's special about static arrays, per se, which may be why you're having trouble finding good write-ups about them. Correct me if I'm wrong, but I wonder if it's the "static" part that you're most interested in? Basically, static means that the member exists at the class level rather than the instance level, so a static array would be an array that belongs to the class (rather than an instance of the class).

Example:

public class Foo
{
   public static int[] Numbers {get; set;}
}

public class Bar
{
   public int[] Numbers {get;set;}
}

public class Program
{
     public void Main()
     {
// NOTE: Foo itself has this array
          Foo.Numbers = new int[]{1,2,3};

// NOTE: it's this particular instance of a Bar that has this array
           Bar bar = new Bar();
           bar.Numbers = new int[]{1,2,3};

     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文