Kod parçacığı işlevleri
İle kullanılabilecek üç işlevi vardır Visual C# kod parçacıkları.İşlevler belirtilen İşlev öğesi (IntelliSense kod parçacıkları) kod parçacığını öğesi.Kod parçacığı oluşturma konusunda daha fazla bilgi için bkz: Kod parçacıkları.
İşlevler
Aşağıdaki tabloda kullanılmak üzere kullanılabilir işlevler açıklanır Function kod parçacıkları öğesi.
İşlev |
Description |
Dil |
---|---|---|
GenerateSwitchCases(EnumerationLiteral) |
Belirtilen numaralandırma üyeleri için bir switch ifadesi ve büyük/küçük harf ifadeleri kümesi oluşturur EnumerationLiteral parametresi.EnumerationLiteral Bir numaralandýrma kalýbýna bir başvuru ya da bir numaralandırma türü parametresi olmalıdır. |
Visual C# |
ClassName() |
Eklenen Parçacık içeren sınıf adını döndürür. |
Visual C# |
SimpleTypeName(TypeName) |
Azaltır TypeName en basit şekliyle Parçacık çağrıldı.%0 bağlam parametresi. |
Visual C# |
Örnek
Aşağıdaki örnek, nasıl kullanılacağını gösterir GenerateSwitchCases işlevi.Ne zaman bu Parçacık eklenir ve bir numaralandırma girilir $switch_on$ değişmez, $cases$ sabit değer üretir bir case deyimi her numaralandırma değeri.
<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>switch</Title>
<Shortcut>switch</Shortcut>
<Description>Code snippet for switch statement</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>expression</ID>
<ToolTip>Expression to switch on</ToolTip>
<Default>switch_on</Default>
</Literal>
<Literal Editable="false">
<ID>cases</ID>
<Function>GenerateSwitchCases($expression$)</Function>
<Default>default:</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
switch ($expression$)
{
$cases$
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Aşağıdaki örnek, nasıl kullanılacağını gösterir ClassName işlevi.Bu Parçacık eklendiğinde, $classname$ hazır kod dosyasında bu konumdaki kapsayan sınıfın adı ile değiştirilir.
<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Common constructor pattern</Title>
<Shortcut>ctor</Shortcut>
<Description>Code Snippet for a constructor</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<Default>int</Default>
</Literal>
<Literal>
<ID>name</ID>
<Default>field</Default>
</Literal>
<Literal default="true" Editable="false">
<ID>classname</ID>
<ToolTip>Class name</ToolTip>
<Function>ClassName()</Function>
<Default>ClassNamePlaceholder</Default>
</Literal>
</Declarations>
<Code Language="vjsharp" Format="CData">
<![CDATA[
public $classname$ ($type$ $name$)
{
this._$name$ = $name$;
}
private $type$ _$name$;
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Bu örnek, nasıl kullanılacağını gösterir SimpleTypeName işlevi.Bu parçacık bir kod dosyasına eklendiğinde, $SystemConsole$ değişmez en basit biçimi ile değiştirilecek Console Parçacık çağrıldı.%0 içerik türü.
<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Console_WriteLine</Title>
<Shortcut>cw</Shortcut>
<Description>Code snippet for Console.WriteLine</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>SystemConsole</ID>
<Function>SimpleTypeName(global::System.Console)</Function>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
$SystemConsole$.WriteLine();
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Ayrıca bkz.
Başvuru
İşlev öğesi (IntelliSense kod parçacıkları)