0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Visual Studio コードスニペット

Posted at

コードスニペット

コードスニペットは、コードファイルに追加できる再利用可能なコードの小さなブロック。

既定コードスニペット(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

プライベート set アクセサーを使用して読み取り専用プロパティを作成する。

public int MyProperty { get; private set; }

sim

static intMain メソッドの宣言を作成する。

static int Main(string[] args)
{

	return 0;
}

struct

struct 宣言を作成する。

public struct MyStruct
{
    
}

svm

static voidMain メソッドの宣言を作成する。

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)
{
    
}

カスタムコードスニペット

コードスニペットは独自に定義して作成することができる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?