I am manually converting this Java code to 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#:
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#.
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>
. The public API is a bit different, but the end goal is the same.
Note there's no need for the AsEnumerable
call though. Since List<T>
implements IEnumerable<T>
you can just write:
IEnumerable<BigInteger> primes = PRIMES;
That said, calling AsEnumerable
isn't really don't anything wrong or costly either.
您的代码看起来不错,但是请注意,可以通过索引访问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':
private static readonly IList<System.Numerics.BigInteger> PRIMES = new System.Numerics.BigInteger[] { 10007, 10009, 10037, 10039 };
internal IEnumerator<System.Numerics.BigInteger> primes = PRIMES.GetEnumerator();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.