是string.ElementAt()O(1)? [英] Is string.ElementAt() O(1)?

查看:169
本文介绍了是string.ElementAt()O(1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据的言论,

如果源类型实现IList,该实现用于获取指定索引处的元素。否则,此方法获得指定的元素。

If the type of source implements IList, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.

String 不执行的IList< T> 。这是否意味着这将是一个 O(N)运行,如果我宣布类似,

String does not implement IList<T>. Does that mean this will be an O(n) operation if I declare something like,

IEnumerable<char> myString = "stringy";



?

推荐答案

的ElementAt 当应用于类型,它是一个字符串将是一个O(n)操作。它不执行的IList<焦炭> ,因此的ElementAt 不会做任何优化,并通过代替枚举的IEnumerable<焦炭> ,直到达到规定的指标。

ElementAt when applied to a type which is a string will be an O(N) operation. It does not implement IList<char> and hence ElementAt won't do any optimizations on it and instead enumerates through the IEnumerable<char> until it reaches the specified index.

这篇关于是string.ElementAt()O(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆