In Beta 1, String doesn't implement IEnumerable<char>, although it obviously should. Please vote on MSDN for this to be fixed [FDBK1452].
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using RSS
I had already voted on that one, and I'm hoping they'll address it. I'm glad you think it should be done too.
<br>
<br>Actually, I think it should implement IList<char> too (explicitly, of course), since it's indexable, and return true for IsReadOnly (and IsFixedSize, which doesn't exist, but that's another poblem..see FDBK15419).
<br>The easiest way I see to make String IEnumerable<char> is for CharEnumerator to implmented IEnumerator<char>. Then they don't even need to write a new enumerator.
A type is no longer considered CLS compliant if it implements a non-CLS compliant interface. Unfortunately generics fall under the non-CLS compiliant domain. I would imagine they would like to keep the standard types CLS compliant, especially an important one like System.String.
<br>Or maybe I missed an annoucment that generics would be included in the CLS so this is a non-issue. Considering the statement made by MS on that bug posting I'm beginning to think I did.
The compiler certainly lets you implement a generic interface and remain CLS-Compliant. You cannot, however, inherit from a generic base class.
<br>Otherwise, String wouldn't be able to implement IComparable<String>, and Int32 couldn't implement IComparable<Int32>.
<br>This would be detrimental to both the whole CLS and generics.
<br>So, there's no reasy why String can't implement IList<Char> or IEnumerable<Char>.
<br>Also, they keep saying Generics will make their way into the CLS. It'll be interesting to see how some languages handle this.
I'm TAG from FDBK14520
<br>I believe that IList<char> from String is nonsence. Can you tell me why would you use this functionality ? Access read-only string data by index ? How common this kind of usage ?
<br>Yep. IList<char> for StringBuilder is cool, you will be able to use not a special class - but IList interface to manipulate string data, remove/add/replace/enumerate characters. This is usefull and most-likeky will be used feature.
<br>As well IEnumerator<char> for CharEnumerator is low-cost and intitive addition.
<br>But once more, I do not see reasons to add ReadOnly and FixedSize IList<char> for String. Only Rtwo methods from IList<char> interface will be used - IndexOf(char x) and Item[int index].
<br>But two others methods (Insert(int, T), RemoveAt(int)) can result in errors that will be able to detect only in runtime. So ?
<br>In my option - you must not implement interface if you can not follow contacts defined fully. I dislike the way how ReadOnly collections implemented. I preffer to have seperate interfaces for read-only collections views and mutable one.
<br>Current ICollection<T> sucks. Then IsReadOnly = true only 3 out of 6 methods can be called safely - Count, Contains(T) and CopyTo(T[ ], int). But others 3 will result in runtime errors.
<br>Runtime errors are very costly for development. They need at least two people to detect them - developer and tester. But I preffer compiler + developer detect read-only collections.
<br>Something like
<br>System.Collections.Generic.IReadCollection<T> {
<br>public bool Contains(T item);
<br>public void CopyTo(T[ ] array, int arrayIndex);
<br>public int Count { get; }
<br> }
<br>System.Collections.Generic.ICollection<T> : IReadCollection<T> {
<br>public bool Remove(T item);
<br>public void Clear();
<br>public void Add(T item);
<br>}
<br>This way everybody who unable to provide read-write access - can implement IReadCollection.
<br>Test for IsReadOnly = !(o is ICollection) and will be performed by compiler - not a CLR
P.S.> Sorry.
<br>I've found flaw in my IReadCollection, ICollection proposal.
<br>Find thich one and correct it ;-)
<br>Core idea to separate readonly collections from read-write at compile-time is a must !!
Well, I agree that an read only collection and list might be better suited to another interface, but then there are also fixed sized lists. You could factor out each set of methods to separate interfaces and have interfaces that combine other interfaces, but I think Microsoft is choosing the simpler approach.
<br>As long as their is a way to detect if a list is ReadOnly at runtime without throwing an exception, I can live with the lack of separate interfaces.
<br>However, there still seems to be no way to check for a fixed size list at runtime without catching an exception. This is a major flaw in the design.
<br>As for string implementing IList, it could be used for a ReverseEnumerator implementation that doesn't have to create a temporary list, since ILists are able to be indexed. I know it adds a lot of methods that do nothing but throw NotSupportedException, but the fact that a string can do a few things that lists can do warrants the implementation, although it's not the end of the world if it doesn't since a String->IList<Char> Adapter is pretty trivial.
<br>A StringBuilder->IList<Char> Adapter is also trivial, but it would be much more useful on the class itself.
<br>So at the very minimum, String should implement IEnumerable<Char>, and StringBuidler should implement IList<Char>. But I see nothing wrong with both implementing IList<Char>.
モデルの子と和気藹々。鳥井プロダクションが運営するモデル<a href="http://www.torii-pro.com/">撮影会</a>。
高級<a href="http://www.rakuten.ne.jp/gold/auc-ibizastore/">レギンス</a>専門店のイビザストア。お買い上げ金額5000円以上で送料無料!レギンスとカラータイツの専門店です。
ネイティブによるスピーディな授業はまさに圧巻の<a href="http://www.naikara.com/">英会話スクール</a>。
<a href="http://www.kk-travel.com/" >格安航空券 韓国</a>や海外への格安航空券はKKトラベルで。
<a href="http://www.jns-online.com/">為替</a>取引、外為、オンライントレードのことなら日本初の外国為替取引専門会社、JNS。
<a href="http://www.internship.or.jp/" >語学留学</a>やワーホリとは違う効果あり。使える英語が身につくインターンシップならiiP!
提案するデザイナーズリフォーム・<a href="http://www.dommyborder.com/">珪藻土</a>リフォームをアトリエ・サーバスは提案します。
キャバクラ情報の<a href="http://www.after.co.jp/">求人</a>情報など。新宿ほか全国の情報を掲載しています。
リフォームをサポートするリフォーム会社ならグローバルネット。トータル<a href="http://www.3-v.jp/">リフォーム 千葉</a>・東京でする際に。
東京、大阪、名古屋で英会話レッスン。マンツーマン<a href="http://www.7act.com/" >英会話スクール</a>の7アクト。