Jér*_*émy 1 c# indexer visual-studio
我在C#中的类中使用了一个索引器,但我想知道默认情况下是否有用于制作索引器的快捷方式(例如' cwtab tab' Console.WriteLine()).有谁知道这是否存在?
这是"Person"类的代码(带索引器):
public string SurName { get; set; }
public string FirstName { get; set; }
public string Birthplace { get; set; }
public string this[int index]
{
set
{
switch (index)
{
case 0:
this.SurName = value;
break;
case 1:
this.FirstName = value;
break;
case 2:
this.Birthplace = value;
break;
default:
throw new ArgumentOutOfRangeException("index");
}
}
get
{
switch (index)
{
case 0: return this.SurName;
case 1: return this.FirstName;
case 2: return this.Birthplace;
default:
throw new ArgumentOutOfRangeException("index");
}
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
-Jérémy
索引
创建索引器声明.
在类或结构中.
所以,输入ind并打Tab两次.这会产生;
public object this[int index]
{
get { /* return the specified index here */ }
set { /* set the specified index to value here */ }
}
Run Code Online (Sandbox Code Playgroud)
但是,还有一个片段可以自动填充get和set吗?
嗯,我之前没试过,但我打开了propfull.snippet,似乎;
....
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set { $field$ = value;}
}
....
Run Code Online (Sandbox Code Playgroud)
而且indexer.snippet貌似;
....
....
<Code Language="csharp"><![CDATA[$access$ $type$ this[$indextype$ index]
{
get {$end$ /* return the specified index here */ }
set { /* set the specified index to value here */ }
}]]>
....
Run Code Online (Sandbox Code Playgroud)
所以,如果你定义<Literal><ID>field</ID>...</Literal>了你的部分indexer.snippet,如果你改变了它的getter和setter之类的;
public object this[int index]
{
get { return $field$; }
set { $field$ = value; }
}
Run Code Online (Sandbox Code Playgroud)
如果一切顺利,这可能会奏效.顺便说一句,这是有效的,它将创建一个除索引器之外的私有字段.这些片段位于C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\1033\Visual C#Visual Studio 2012的文件夹中.
| 归档时间: |
|
| 查看次数: |
430 次 |
| 最近记录: |