初始化列表直列 [英] Initializing list inline

查看:123
本文介绍了初始化列表直列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这样做时,一个奇怪的错误:(.NET 2.0)

 公共覆盖列表<弦乐> getSpaceballs 
{
{返回新的List<串GT;(){ABC,高清,EGH}; }
}



VS是要求; ()。为什么?



我肯定能做到这一点:

 公众覆盖字符串[] getSpaceballs 
{
{返回新的String [] {ABC,高清,EGH}; }
}


解决方案

第一个选项是不。合法的:)



您只能做这种类型的数组初始化剂



- 编辑:见< A HREF =htt​​p://stackoverflow.com/users/34211/andrew-hare>安德鲁野兔的后(和其他人,下同); 。它只是在V3和最多可用



- 再次编辑:



只是要清楚,如果你的编译器是3或更大,您可以针对2.0,要得到这个工作(因为它的下面编译成安德鲁显示的代码)。但是,如果你的编译器是2,那么你就不能。


I'm getting a weird error when doing this: (.net 2.0)

public overrides List<String> getSpaceballs
{
    get { return new List<String>() { "abc","def","egh" }; }
}

VS is asking for ; after (). Why?

I sure can do this:

public overrides string[] getSpaceballs
{
    get { return new string[] { "abc","def","egh" }; }
}

解决方案

The first option is not legal :)

You can only do that type of initialiser on arrays.

-- Edit: See Andrew Hare's post (and others, below); it is only available in v3 and up.

-- Edit again:

Just to be clear, if your compiler is of 3 or greater, you can target 2.0, to get this to work (because it's compiled down to the code that Andrew shows, below). But if your compiler is 2, then you can't.

这篇关于初始化列表直列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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