コード入力量を減らす!VisualC#で使えるコードスニペットまとめ
コードスニペットとは?
コードスニペットとは、VisualStudioで特定のキーワードを入力し、Tabキーを2回押すことによって定型文を挿入することができるコードの自動生成機能です。
たとえば「while」と入力し、Tabキーを2回連続で押すと
このようにwhileループを一発で作成することができます。
さらにTabキーを押すことによって定型文の中でも変更すべきところにフォーカスが行き、簡単に定型文を挿入することができます。
VisualC#のコードスニペット一覧
#if
#ifを作成することができます。
#if true #endif
#region
#regionを作成することができます。
#region MyRegion #endregion
~
デストラクタを作成することができます。
~Program() { }
attribute
属性を作成することができます。
[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
{
}
class
クラスを作成します。
class MyClass
{
}
ctor
コンストラクターを作成します。
public Program()
{
}
cw
コンソール出力を作成します。
Console.WriteLine();
do
do-whileループを作成します
do { } while (true);
else
elseステートメントを作成します
else
{
}
enum
列挙体を作成します
enum MyEnum
{
}
equals
比較メソッド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); } // override object.GetHashCode public override int GetHashCode() { // TODO: write your implementation of GetHashCode() here throw new NotImplementedException(); return base.GetHashCode(); }
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 IInterface
{
}
invoke
nullチェック付きイベント呼び出しを作成します。
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); } } public class MyViewIterator { readonly Program outer; internal MyViewIterator(Program outer) { this.outer = outer; } // TODO: provide an appropriate implementation here public int Length { get { return 1; } } public ElementType this[int index] { get { // // TODO: implement indexer here // // you have full access to Program privates // throw new NotImplementedException(); return default(ElementType); } } public System.Collections.Generic.IEnumerator<ElementType> GetEnumerator() { for (int i = 0; i < this.Length; i++) { yield return this[i]; } } }
lock
排他制御を行うことができます。
lock (this) { }
mbox
MessageBox(古い)を作成します。
global::System.Windows.Forms.MessageBox.Show("Test");
namespace
名前空間を作成します。
namespace MyNamespace
{
}
prop
自動実装プロパティを作成します。
public int MyProperty { get; set; }
propfull
プロパティ付きフィールドを作成します。
private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } }
propg
読み取り専用プロパティを作成します
public int MyProperty { get; private set; }
sim
Main関数を作成します。
static int Main(string[] args) { return 0; }
struct
構造体を作成します
struct MyStruct
{
}
svm
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
{
}
unsafe
unsafe
{
}
using
usingステートメントを作成します。
using (resource)
{
}
while
whileループを作成します。
while (true) { }
感想
propfullはよく使うと思う