[英]Shortcut for indexers in C#
I'm using an indexer in a class in C#, but I'm wondering if there is by default a shortcut for making indexers (like for example ' cw
tab tab' for Console.WriteLine()
). 我在C#中的类中使用索引器,但我想知道默认情况下是否存在用于创建索引器的快捷方式(例如
Console.WriteLine()
“ cw
tab tab”)。 Does anyone knows if this exists? 有谁知道这是否存在?
Here's my code (with indexer) for the class 'Person': 这是我的“ 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");
}
}
}
Thanks in advance! 提前致谢!
-Jérémy -杰里米
From Visual C# Code Snippets 从Visual C#代码段
indexer
索引器
Creates an indexer declaration.
创建一个索引器声明。
Inside a class or a struct.
在类或结构中。
So, type ind
and hit Tab
twice. 因此,键入
ind
并按两次Tab
。 This generates; 这产生了;
public object this[int index]
{
get { /* return the specified index here */ }
set { /* set the specified index to value here */ }
}
However, is there also a snippet that fills in the get and set automatically?
但是,是否还有一个自动填写get和set的代码段?
Hmm , I did not tried this before but I opened propfull.snippet
and it seems like; 嗯 ,我之前没有尝试过,但是我打开了
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;}
}
....
And indexer.snippet
looks like; 和
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 */ }
}]]>
....
So, if you define <Literal><ID>field</ID>...</Literal>
part in your indexer.snippet
and if you change it's getter and setter like; 因此,如果您在
indexer.snippet
定义了<Literal><ID>field</ID>...</Literal>
部分,并且如果您进行了更改,则它像getter和setter一样;
public object this[int index]
{
get { return $field$; }
set { $field$ = value; }
}
this might work if everything is good. 如果一切都很好,这可能会起作用。 By the way, it this works, it will create a private field additional to indexer.
顺便说一句,它可以正常工作,它将创建除索引器之外的私有字段。 Those snippets are in
C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC#\\Snippets\\1033\\Visual C#
folder for Visual Studio 2012. 这些片段位于Visual Studio 2012的
C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC#\\Snippets\\1033\\Visual C#
文件夹中。
您可以创建代码段,并查看以下页面: https : //msdn.microsoft.com/zh-cn/library/ms165394.aspx或此页面以查看现有代码段https://msdn.microsoft.com/en -us / library / z41h7fat.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.