コードスニペット
コードスニペットは、コードファイルに追加できる再利用可能なコードの小さなブロック。
既定コードスニペット(C#)
Visual Studio では、既定のコードスニペットを利用することができる。ただし、どこでも利用できるというわけではない。例えばコンストラクタを作成する ctor
スニペットはクラス内でのみ利用することができる。
#if
#if
ディレクティブと #endif
ディレクティブを作成する。
#if true
#endif
#region
#region
ディレクティブと #endregion
ディレクティブを作成する。
#region MyRegion
#endregion
~
ファイナライザーを作成する。
~MyClass()
{
}
attribute
Attribute
から派生するクラスの宣言を作成する。
[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string positionalString;
// This is a positional argument
public MyAttribute(string positionalString)
{
this.positionalString = positionalString;
// TODO: Implement code here
throw new NotImplementedException();
}
public string PositionalString
{
get { return positionalString; }
}
// This is a named argument
public int NamedInt { get; set; }
}
checked
checked
ブロックを作成する。
checked
{
}
class
クラスの宣言を作成する。
class MyClass
{
}
ctor
コンストラクターを作成する。
public MyClass()
{
}
cw
WriteLine
への呼び出しを作成する。
Console.WriteLine();
do
do while
ループを作成する。
do
{
} while (true);
else
else
ブロックを作成する。
else
{
}
enum
enum
宣言を作成する。
enum MyEnum
{
}
equals
Object
クラスに定義された Equals
メソッドをオーバーライドするメソッド宣言を作成する。
// override object.Equals
public override bool Equals(object obj)
{
//
// See the full list of guidelines at
// http://go.microsoft.com/fwlink/?LinkID=85237
// and also the guidance for operator== at
// http://go.microsoft.com/fwlink/?LinkId=85238
//
if (obj == null || GetType() != obj.GetType())
{
return false;
}
// TODO: write your implementation of Equals() here
throw new NotImplementedException();
return base.Equals(obj);
}
exception
exception
(既定では Exception
) から派生するクラスの宣言を作成する。
[Serializable]
public class MyException : Exception
{
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner) : base(message, inner) { }
protected MyException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
for
for
ループを作成する。
for (int i = 0; i < length; i++)
{
}
foreach
foreach
ループを作成する。
foreach (var item in collection)
{
}
forr
ループ変数をデクリメントする for
ループを作成する。
for (int i = length - 1; i >= 0; i--)
{
}
if
if
ブロックを作成する。
if (true)
{
}
indexer
インデクサーの宣言を作成する。
public object this[int index]
{
get { /* return the specified index here */ }
set { /* set the specified index to value here */ }
}
interface
interface
宣言を作成する。
interface IInterface
{
}
invoke
イベントを安全に呼び出すブロックを作成する。
EventHandler temp = MyEvent;
if (temp != null)
{
temp();
}
iterator
反復子を作成する。
public System.Collections.Generic.IEnumerator<ElementType> GetEnumerator()
{
throw new NotImplementedException();
yield return default(ElementType);
}
iterindex
カスタムイテレータクラスへのプロパティアクセサを作成する。
public MyViewIterator MyView
{
get
{
return new MyViewIterator(this);
}
}
lock
lock
ブロックを作成する。
lock (this)
{
}
mbox
System.Windows.Forms.MessageBox.Show
への呼び出しを作成する。
System.Windows.Forms.MessageBox.Show("Test");
namespace
namespace
宣言を作成する
namespace MyNamespace
{
}
prop
プロパティ宣言を作成する。
public int MyProperty { get; set; }
propfull
get
および set
アクセサーを持つプロパティの宣言を作成する。
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
propg
プライベート se
t アクセサーを使用して読み取り専用プロパティを作成する。
public int MyProperty { get; private set; }
sim
static int
の Main
メソッドの宣言を作成する。
static int Main(string[] args)
{
return 0;
}
struct
struct
宣言を作成する。
public struct MyStruct
{
}
svm
static void
の Main
メソッドの宣言を作成する。
static void Main(string[] args)
{
}
switch
switch
ブロックを作成する。
switch (switch_on)
{
default:
}
try
try-catch
ブロックを作成する。
try
{
}
catch (Exception)
{
throw;
}
tryf
try-finally
ブロックを作成する。
try
{
}
finally
{
}
unchecked
unchecked
ブロックを作成する。
unchecked
{
}
unsafe
unsafe
ブロックを作成する。
unsafe
{
}
using
using
ディレクティブを作成する。
using (resource)
{
}
while
while
ループを作成する。
while (true)
{
}
カスタムコードスニペット
コードスニペットは独自に定義して作成することができる。