私はinternal
キーワードを理解しようとしていましたが、いつ使用する必要がありますか。幸い、私は深さのジョンスキートの本C#で読んでいるよう今日、イテレータを実装する方法について第3版は、p.161-162にinternal
のための使用があります:6.32Jon Skeetの本の中のこの例で使われているinternalキーワードは何故ですか?
なぜinternal IterationSampleIterator
6.3
class IterationSampleIterator : IEnumerator
{
IterationSample parent;
int position;
internal IterationSampleIterator(IterationSample parent)
{
this.parent = parent;
position = -1;
}
public bool MoveNxt()
{
if (position != parent.values.Length)
{
position++'
}
return position < parent.values.Length;
}
public object Current
{
get
{
if (position == -1 ||
position == parent.values.Length)
{
throw new InvalidOperationException();
}
int index = position + parent.startingPoint;
index = index % parent.values.Length;
return parent.values[index];
}
}
public void Reset()
{
position = -1;
}
}
リスト
using System;
using System.Collections;
public class IterationSample : IEnumerable
{
object[] values;
int startingPoint;
public IterationSample(object[] values, int startingPoint)
{
this.values = values;
this.startingPoint = startingPoint;
}
public IEnumerator GetEnumerator()
{
throw NotImplementedException();
}
}
?私が理解するように、internal
は通常public
の代わりです。 public IterationSampleIterator
がpublic
をinternal
に置き換えて解決できる問題を引き起こす原因を理解するのを助けてください。
私は自分自身に逆の質問をするべきだと思います。 'internal'で十分なら'なぜ 'public'を使うのですか? – JLRishe