隐式类型类成员

发布于 2024-11-09 01:52:42 字数 444 浏览 0 评论 0原文

可能的重复:
在方法之外使用 var

我对此进行了一些搜索,但是我不太确定搜索词,所以没有找到任何东西。

为什么我不能这样做:

class foo
{
    var bar = new Dictionary<string, string>();
}

猜想一定有一个很好的理由,但我想不出来!

我对推理更感兴趣,而不是因为“C# 不允许”的答案。

编辑:编辑 Dictionary 声明,抱歉(只是示例中的拼写错误)!

Possible Duplicate:
Using var outside of a method

I've searched for this a bit, but am not too sure of the search terms so didn't find anything.

Why can't i do this:

class foo
{
    var bar = new Dictionary<string, string>();
}

Guessing there must be a very good reason, but i can't think of it!

I'm more interested in the reasoning, rather than the because "C# doesn't let you" answer.

EDIT: Edited Dictionary declaration, sorry (just a typo in the example)!

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

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

发布评论

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

评论(5

彻夜缠绵 2024-11-16 01:52:43

字典需要两个参数,一个键类型和一个值类型。

var bar = new Dictionary<string, string>();

A dictionary needs two parameters, a key type and a value type.

var bar = new Dictionary<string, string>();
乄_柒ぐ汐 2024-11-16 01:52:43

“字典”是指键/值对的集合。在现实世界中,世界词典是一本包含“单词”和“定义”的书,这里“单词”是键,“定义”是值。所以很明显,在实例化 Dictionary 时不能忽略“值”。

"Dictionary" means a collection of key/value pairs. In real-world, a dictionary of worlds is a book containing "Words" and "Definitions", here "Word" is key and "Definition" is value. So this is obvious that you can't ignore "value" when instantiating a Dictionary<TKey, TValue>.

后eg是否自 2024-11-16 01:52:43

字典是一个用于将一组键映射到一组值的类,因此您需要为键和值指定类型参数。例如,如果您想根据股票代码查找股价,您可以使用:

var stocks = new Dictionary<string, decimal>();
stocks.Add("MSFT", 25.5M);
stocks.Add("AAPL", 339.87M);

A dictionary is a class for mapping a set of keys to a set of values, so you need to specify a type parameter for both the key and value. For example, if you want to lookup a share price based on its stock symbol, you might use:

var stocks = new Dictionary<string, decimal>();
stocks.Add("MSFT", 25.5M);
stocks.Add("AAPL", 339.87M);
乞讨 2024-11-16 01:52:42

2个原因:

  1. 字典需要Key和Value泛型参数
  2. 你不能直接在类中编写这样的变量=>你可以使用字段、属性或方法

所以:

class foo
{
    private Dictionary<string, string> bar = new Dictionary<string, string>();
}

至于为什么你不能这样做:

class foo
{
    private var bar = new Dictionary<string, string>();
}

Eric Lippert 已在 博客文章

2 reasons:

  1. The Dictionary requires Key and Value generic parameters
  2. You cannot write variables like this directly inside a class => you could use fields, properties or methods

So:

class foo
{
    private Dictionary<string, string> bar = new Dictionary<string, string>();
}

As to why you cannot do this:

class foo
{
    private var bar = new Dictionary<string, string>();
}

Eric Lippert has covered it in a blog post.

灵芸 2024-11-16 01:52:42

您没有指定键的类型,它应该是:

class foo
{
    Dictionary<string,string> bar = new Dictionary<string,string>();
}

编辑:并且在类字段的情况下不允许使用“var”。

You did not specify a type for the key, it should be:

class foo
{
    Dictionary<string,string> bar = new Dictionary<string,string>();
}

Edit: And it's not allowed to use "var" in case of class fields.

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