投稿数 - 437, コメント - 52841, トラックバック - 156

CA1002

public List<Hoge> GetHogeList()
{
    return new List<Hoge>();
}

コード分析的にはこのコードは駄目らしい。「CA1002 ジェネリック リストを公開しません」だ。原文は「Do not expose generic lists 」なので、「ジェネリックリストを公開するな(公開しないでください)」がスッキリな訳だと思うが。

List<T> をパブリックに使うのは駄目で、あくまでプライベートに使いなさいという事かな。だったら ArrayList で十分だ。プライベートに使っている間は ArrayList に何が入っているのか完全に把握しているから、「厳密に型指定できるんだよ!」というのは List<T> を使用する動機としては弱い。

「パフォーマンスが違うんだよ!」というのも何か弱い。それでも ArrayList よりは List<T> のほうがマシと言えばマシだが。

いちいちコレクションクラスを作る面倒が大幅に削減できると思ったのだが、勘違いか?

public class HogeCollection : List<Hoge> { };

List<T> の唯一の有効な使い方はこれしかない?確かにコレクションクラスを作るのが非常に楽になった。

投稿日時 : 2006年9月22日 19:21

フィードバック

# re: CA1002

はじめまして。こっそり色々参考にさせていただいてます。

コード分析は使ったこと無いので見当違いかもしれませんが、戻り値の型を IList<T> にすればいいって話では無いんですかね?
List<T>だと実装を公開してることになるので、インターフェースだけ公開するようにしろ、みたいな。
2006/09/22 20:29 | aki.

# re: CA1002

「genegricsが使えない.NET族もいるから遠慮しろ」なんてー消極的な理由だったり ^^;
2006/09/22 20:44 | επιστημη

# re: CA1002

>コード分析は使ったこと無いので見当違いかもしれませんが、戻り値の型を IList<T> にすればいいって話では無いんですかね?
>List<T>だと実装を公開してることになるので、インターフェースだけ公開するようにしろ、みたいな。

お~、その発想はなかったですね。試しにやってみた所、OKが出ました!
何か釈然としませんが、言われてみると納得です。ありがとうございます。

>「genegricsが使えない.NET族もいるから遠慮しろ」なんてー消極的な理由だったり ^^;

この理由もありそうですね。あれ?って事はジェネリクスを使うと CLS 準拠じゃなくなる?


しかし、MSDN の日本語訳は分かりにくいなぁ。
「外部から参照可能な System.Collections.Generic.List 型のメンバを含む型が System.Collections.Generic.List 型を返すか、その署名に System.Collections.Generic.List パラメータが含まれています。」
「署名」ってなんだ?と思ったら「シグネチャ」の事でした。
2006/09/22 21:54 | 囚人

# [.NET] CA1002

なんか誰も書いてないみたい ( というか、 ぐぐる様に引っかからない ) なので、 メモ。 囚人のジレンマな日々 の CA1002 で、 コード分析 ( FxC

# re: CA1002

古い記事にトラックバック掛けて恐縮です。
ぐぐる様に聞いたら、 ここがトップだったもので… f(^^;
http://www.google.co.jp/search?hl=ja&q=CA1002+System.Collections.Generic.List&lr=lang_ja
2008/02/25 20:01 | biac

# If you are going for most excellent contents like I do, simply pay a quick visit this web site daily since it gives feature contents, thanks

If you are going for most excellent contents like I do, simply pay a quick visit this web
site daily since it gives feature contents, thanks

# It's great that you are getting thoughts from this article as well as from our dialogue made here.

It's great that you are getting thoughts from
this article as well as from our dialogue made here.

# Hello, I enjoy reading through your post. I like to write a little comment to support you.

Hello, I enjoy reading through your post. I like to write a
little comment to support you.

# Hi there! I realize this is kind of off-topic however I had to ask. Does managing a well-established website like yours require a massive amount work? I'm completely new to writing a blog but I do write in my journal everyday. I'd like to start a blog s

Hi there! I realize this is kind of off-topic however I had to
ask. Does managing a well-established website
like yours require a massive amount work?
I'm completely new to writing a blog but I do write in my journal everyday.
I'd like to start a blog so I can easily share my experience and views online.
Please let me know if you have any kind of recommendations or tips for new aspiring bloggers.

Appreciate it!

# We are a gaggle of volunteers and starting a brand new scheme in our community. Your website offered us with valuable information to work on. You've performed an impressive job and our entire neighborhood might be grateful to you.

We are a gaggle of volunteers and starting a brand new scheme in our community.

Your website offered us with valuable information to work on. You've performed
an impressive job and our entire neighborhood might be grateful to you.

# We are a gaggle of volunteers and starting a brand new scheme in our community. Your website offered us with valuable information to work on. You've performed an impressive job and our entire neighborhood might be grateful to you.

We are a gaggle of volunteers and starting a brand new scheme in our community.

Your website offered us with valuable information to work on. You've performed
an impressive job and our entire neighborhood might be grateful to you.

# We are a gaggle of volunteers and starting a brand new scheme in our community. Your website offered us with valuable information to work on. You've performed an impressive job and our entire neighborhood might be grateful to you.

We are a gaggle of volunteers and starting a brand new scheme in our community.

Your website offered us with valuable information to work on. You've performed
an impressive job and our entire neighborhood might be grateful to you.

# We are a gaggle of volunteers and starting a brand new scheme in our community. Your website offered us with valuable information to work on. You've performed an impressive job and our entire neighborhood might be grateful to you.

We are a gaggle of volunteers and starting a brand new scheme in our community.

Your website offered us with valuable information to work on. You've performed
an impressive job and our entire neighborhood might be grateful to you.

コメントの投稿

タイトル  
名前  
URL
コメント