Help us understand the problem. What is going on with this article?

型の別名定義(using エイリアス ディレクティブ)を入れ子にする (c#)

前提

  • c# 5~

試したこと

  • using エイリアス ディレクティブを使用して、型の別名を定義し、その別名を使用して新たに別名を定義してみました。

エラー

using SpreadSheet = Matrix<string>;
using SpreadSheetBook = Dictionary<string, SpreadSheet>;
namespace SpreadSheetManager {
namespace SpreadSheetManager {
    using SpreadSheet = Matrix<string>;
    using SpreadSheetBook = Dictionary<string, SpreadSheet>;

可能

using SpreadSheet = Matrix<string>;
namespace SpreadSheetManager {
    using SpreadSheetBook = Dictionary<string, SpreadSheet>;
namespace SpreadSheetManager {
    using SpreadSheet = Matrix<string>;
    namespace xxx {
        using SpreadSheetBook = Dictionary<string, SpreadSheet>;

正解

namespace SpreadSheetManager {
    using SpreadSheet = Matrix<string>;
    using SpreadSheetBook = Dictionary<string, Matrix<string>>;
using SpreadSheet = Matrix<string>;
using SpreadSheetBook = Dictionary<string, Matrix<string>>;
namespace SpreadSheetManager {

別解

namespace SpreadSheetManager {
    public class SpreadSheet : Matrix<string> { }
    public class using SpreadSheetBook : Dictionary<string, Matrix<string>> { }
  • 少し付け加えたくなることってありますよね。

解ったこと

  • 同じnamespace階層では、定義した別名は、他の別名の定義で使用できません。
  • 親のnamespace階層で定義した別名は、子階層の別名定義で使用可能です。
  • using エイリアス ディレクティブを、入れ子にする必要はありません。
  • 別名を付けるような場合は、手を入れたくなることも多いので、結局、継承クラスを作ることになりがちです。
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away