我一直在寻找静态数组的像样的定义。我尝试过使用msdn和c#源代码,但似乎找不到定义。它给出了例子,但没有定义...
有没有人知道静态数组的链接、定义和特征?
发布于 2011-01-04 14:52:21
当你谈论“静态数组”时,你实际上是在谈论两件不同的事情。
一个是static
关键字。当应用于变量时,这意味着变量位于class
级别,并且该类型的每个对象都不会获得自己的实例。
array
只是一种用于保存某种类型的多个值的数据结构。
因此,static array
只是一个类级别的数组,它可以容纳某些数据类型的倍数。
例如:
在您的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];
发布于 2011-01-04 15:31:21
你的意思是固定大小的数组吗?
unsafe struct Foo
{
fixed int Values[8];
}
如果是这样,通过使用固定大小的数组作为查询,您将获得更多的搜索结果:)
发布于 2011-01-04 14:58:17
就我所知,静态数组本身并没有什么特别之处,这可能就是为什么你很难找到关于它们的好文章的原因。如果我错了,请纠正我,但我想知道这是不是你最感兴趣的“静态”部分?基本上,静态意味着成员存在于类级别而不是实例级别,因此静态数组将是属于类的数组(而不是类的实例)。
示例:
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};
}
}
https://stackoverflow.com/questions/4594850
复制相似问题