Search code examples
c#javaenumerator

How to translate this piece of Java code to C#?


Here is the Java code which I want to translate to C#:

public Enumeration getLogHeaders()
{
return logHeaders != null ? logHeaders.elements() : null;
}

logHeaders is a List<String>.

This the translated C# version, but I get a compiler error whenever I try it.

public IEnumerable<string> getLogHeaders()
{
  return logHeaders != null ? logHeaders.GetEnumerator() : Enumerable.Empty<string>();
}

How would I have to change it?


Solution

  • GetEnumerator doesn't return an IEnumerable, it returns the enumerator itself. So remove that part and return logHeaders.

    Here's what I might write:

    public IEnumerable<string> LogHeaders
    {
       get { return logHeaders ?? Enumerable.Empty<string>(); }
    }