怎么做“编译时间”记录数组的初始化? [英] How to do "Compile time" initialization of record array?

查看:61
本文介绍了怎么做“编译时间”记录数组的初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回到旧的与C相关的日子,我曾经做过像......


struct

{

char描述[20]; < br $>
浮动价格;

}商品[] =

{

{" Apple",1.99},

{" Banana",2.04}

};


如果我想做类似的事情(和我一样),有没有人提示

显然是新的C#程序员?


非常感谢任何帮助,

John Kelsey

解决方案

这看起来像一个哈希表。所以我会这样做...


哈希表hashtable = new Hashtable();

hashtable.add(" Apple",1.99);

hashtable.add(" Banana",2.04);


但是你必须知道密钥(例如''Apple''或''Banana'' )

在哈希表中引用这些项目。我建议用你想要的属性创建一个新的

对象。然后创建一个ArrayList,并将
添加到ArrayList。


Dave,


我很欣赏这个回复,但我正在寻找一些不同的东西。


我的数组在运行前被初始化了。我知道我可以将

记录添加到集合中。作为程序执行的一部分,但是在C中,

数组包含了我想要的值而无需编写一个可执行的

语句。


这可能是C#不支持的一个微妙点...它可能只是我的

编程风格......


我想这归结为

int i = 32;




int i;
之间的区别
....

i = 32;

这是否更清楚?

Dave <直流***** @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

这看起来像一个哈希表。所以我会这样做...

Hashtable hashtable = new Hashtable();
hashtable.add(" Apple",1.99);
hashtable.add(" Banana" ;,2.04);

然而,您必须知道密钥(例如''Apple''或''Banana'')才能在哈希表中引用这些项目。我建议用你想要的属性创建一个新的
对象。然后创建一个ArrayList并将该对象添加到ArrayList。



John Kelsey< ke ***** @ tacits.com>写道:

返回旧字样与C的日子,我曾经做过类似的事情...

结构
{描述[20];
浮动价格;
}项目[] =
{
{" Apple",1.99},
{" Banana",2.04}
};

如果我想要的话做一些类似的(和我做的),有没有人提示明显是新的C#程序员?




嗯,首先我要分开声明类型使用

吧。然后我会提供一个构造函数来获取描述和

的价格,并使用它。例如:


使用系统;


公共类物品

{

字符串描述;

公共字符串描述

{

get {return description; }

}


//注意更改使用小数类型,

//这通常比float更合适< br $>
//金钱

十进制价格;

公共小数价格

{

得到{返回价格; }

}


public Item(字符串描述,小数价格)

{

这个。 description = description;

this.price = price;

}

}


公共课测试

{

static void Main()

{

Item [] items =

{

新商品(Apple,1.99m),

新商品(Banana,2.0m)

};


foreach(项目中的项目项目)

{

Console.WriteLine(" {0}费用{ 1}",

item.Description,

item.Price);

}

}

}


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件/>


Back in the "old" days with C, I used to do something like...

struct
{
char Description[20];
float price;
} Items[] =
{
{"Apple", 1.99},
{"Banana", 2.04}
};

If I wanted to do something similiar (and I do), does anyone have hints for
an obviously new C# programmer?

Any help is greatly appreciated,
John Kelsey

解决方案

That looks like a hashtable. So i would do...

Hashtable hashtable = new Hashtable();
hashtable.add("Apple", 1.99);
hashtable.add("Banana", 2.04);

However you will have to know the key (e.g. ''Apple'' or ''Banana'') to
refer to these items in the hashtable. I would recomend creating a new
object with properties that you desire. Then create an ArrayList and
add that object to the ArrayList.


Dave,

I appreciate the response but I was looking for something slightly
different.

My array was initialized before runtime. I understand that I can add
records to a "collection" as part of the program execution, but in C, the
array contained the values I wanted without having to write one executable
statement.

This may be a subtle point that C# doesn''t support... it may just be my
programming style...

I guess it boils down to the difference between
int i = 32;

and
int i;
....
i = 32;
Is this any clearer?
"Dave" <dc*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

That looks like a hashtable. So i would do...

Hashtable hashtable = new Hashtable();
hashtable.add("Apple", 1.99);
hashtable.add("Banana", 2.04);

However you will have to know the key (e.g. ''Apple'' or ''Banana'') to
refer to these items in the hashtable. I would recomend creating a new
object with properties that you desire. Then create an ArrayList and
add that object to the ArrayList.



John Kelsey <ke*****@tacits.com> wrote:

Back in the "old" days with C, I used to do something like...

struct
{
char Description[20];
float price;
} Items[] =
{
{"Apple", 1.99},
{"Banana", 2.04}
};

If I wanted to do something similiar (and I do), does anyone have hints for
an obviously new C# programmer?



Well, firstly I''d separate the declaration of the type from the use of
it. I''d then provide a constructor which took the description and the
price, and use that. For instance:

using System;

public class Item
{
string description;
public string Description
{
get { return description; }
}

// Note change to use the decimal type,
// which is usually more appropriate than float
// for money
decimal price;
public decimal Price
{
get { return price; }
}

public Item (string description, decimal price)
{
this.description = description;
this.price = price;
}
}

public class Test
{
static void Main()
{
Item[] items =
{
new Item ("Apple", 1.99m),
new Item ("Banana", 2.0m)
};

foreach (Item item in items)
{
Console.WriteLine ("{0} costs {1}",
item.Description,
item.Price);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于怎么做“编译时间”记录数组的初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆