[英]what is the c# equivalent of List and Iterator in Java?
I am manually converting this Java code to C#: 我正在手动将此Java代码转换为C#:
private static final List<BigInteger> PRIMES = Arrays.asList(new BigInteger[]
{ new BigInteger("10007"), new BigInteger("10009"),
new BigInteger("10037"), new BigInteger("10039")});
Iterator<BigInteger> primes = PRIMES.iterator();
and this is my code in C#: 这是我在C#中的代码:
private static readonly List<BigInteger> PRIMES = new List<BigInteger> {
10007, 10009,
10037, 10039 };
IEnumerable<BigInteger> primes = PRIMES.AsEnumerable<BigInteger>();
However, I am not sure if my code is correct. 但是,我不确定我的代码是否正确。 I really do not understand about list and iterator in C#.
我真的不了解C#中的列表和迭代器。
Please anyone help me to convert the code correctly, any assistance is greatly appericated. 请任何人帮助我正确转换代码,我们将为您提供任何帮助。
Many thanks. 非常感谢。
Your code is correct. 您的代码是正确的。
List<T>
is the C# equivalent of the java ArrayList<T>
, and IEnumerable<T>
is more or less the equivalent of the java Iterator<T>
. List<T>
是java ArrayList<T>
的C#等效项,而IEnumerable<T>
或多或少是java Iterator<T>
的等效项。 The public API is a bit different, but the end goal is the same. 公用API有所不同,但最终目标是相同的。
Note there's no need for the AsEnumerable
call though. 注意,尽管没有必要进行
AsEnumerable
调用。 Since List<T>
implements IEnumerable<T>
you can just write: 由于
List<T>
实现IEnumerable<T>
您可以编写:
IEnumerable<BigInteger> primes = PRIMES;
That said, calling AsEnumerable
isn't really don't anything wrong or costly either. 话虽如此,调用
AsEnumerable
并没有错,也没有付出任何代价。
您的代码看起来不错,但是请注意,可以通过索引访问c#中的List,因此根据您的操作,可能不需要等效的迭代器。
Java 'List' is an interface, so the .NET equivalent is 'IList', and the .NET equivalent to Java's Iterator is 'IEnumerator', not 'IEnumerable': Java'List'是一个接口,因此.NET的等效项是'IList',而Java的Iterator的.NET等效项是'IEnumerator',而不是'IEnumerable':
private static readonly IList<System.Numerics.BigInteger> PRIMES = new System.Numerics.BigInteger[] { 10007, 10009, 10037, 10039 };
internal IEnumerator<System.Numerics.BigInteger> primes = PRIMES.GetEnumerator();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.