LoginSignup
1
3

More than 3 years have passed since last update.

C# よく使うusing 名前空間 / (Windows)Form テンプレ作った (Visual Studio使わない人向け)

Last updated at Posted at 2019-11-01

自分用メモ

テキトーなサンプルコードを作るときに重宝するはず。


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

class XXXX : Form
{
    XXXX()
    {
        //Text = ;
        //ClientSize = new Size(,);
        //Controls.Add(xx);
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new XXXX());
    }
}

各名前空間の主な用途

面倒くさいのでコード上にコメントで記載。
正直、用途を忘れたものがある


using System;
using System.Collections;
using System.Collections.Generic;     // ジェネリック: List<xxx>, Dictionary<xxx>, ...
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;                 // Size, Point
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;                      // File
using System.Reflection;
using System.Runtime.InteropServices; // アンマネージコード(Win32API, COMなど)との連携: Marshal
using System.Text;                    // 文字コード処理: Encoding
using System.Text.RegularExpressions; // 正規表現:  Regex, Match
using System.Threading;               // スレッド: Thread
using System.Threading.Tasks;         // タスク:  Task
using System.Windows.Forms;           // フォーム: Form, Button, TextBox, ...

usingディレクティブを使うことのメリット・デメリット

メリット
・短く書ける
 (コードが見やすい。)

デメリット
・どの名前空間のクラスを使っているのかが読み取りづらくなる。
 (意図しないクラスを使ってしまったり、他人に分かりづらいかもしれない。)
コンパイルが多少遅くなるかもしれない(未確認だが、無視できるレベルのはず)

追記:Visual Studio Code (VSCode) スニペット追加

コメント頂いたので、スニペットを作ってみました。(初作成)
usingallとタイプすると出ます(下記のprefixのところで変更できます)。スニペット便利!

スニペットの追加(自作)の仕方:
https://webbibouroku.com/Blog/Article/vscode-snippets

さらに追記:usingだけ入れたい場合に邪魔しないように、ちょっと修正しました。

csharp.json

{
    // Place your snippets for csharp here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    "using directive": {
        "prefix": "using",
        "body": [
            "using "
        ],
        "description": "using directive"
    },
    "CSharp Template": {
        "prefix": "usingall",
        "body": [
            "using System;",
            "using System.Collections;",
            "using System.Collections.Generic;",
            "using System.ComponentModel;",
            "using System.Data;",
            "using System.Diagnostics;",
            "using System.Drawing;",
            "using System.Drawing.Drawing2D;",
            "using System.Drawing.Imaging;",
            "using System.IO;",
            "using System.Reflection;",
            "using System.Runtime.InteropServices;",
            "using System.Text;",
            "using System.Text.RegularExpressions;",
            "using System.Threading;",
            "using System.Threading.Tasks;",
            "using System.Windows.Forms;",
            "",
            "class XXXX : Form",
            "{",
            "    XXXX()",
            "    {",
            "        //Text = ;",
            "        //ClientSize = new Size(,);",
            "        //Controls.Add(xx);",
            "    }",
            "    ",
            "    [STAThread]",
            "    static void Main(string[] args)",
            "    {",
            "        Application.Run(new XXXX());",
            "    }",
            "}",
            ""
        ],
        "description": "Insert the template code"
    }
}

1
3
2

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
1
3