There are no anonymous classes in C# (in the sense you need). If you want to return an instance of a custom implementation of TokenStream
, you need to define it and give it a name.
Eg.:
public MyTokenStream {
public Token nextToken() {
// ...
}
}
public TokenStream plumb() {
return new MyTokenStream();
}
See:
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
http://www.25hoursaday.com/CsharpVsJava.html (anonymous inner classes are listed in the "Wish You Were Here" section; "this section describes language features and concepts that exist in Java and have no C# counterpart").
for reference.
As kan remarked, in C# (and Java 8, too) you would typically use a delegate or a lambda instead. If all that TokenStream does is returning a nextToken, it could be implemented like so:
public class TokenStream
{
}
public class SomeClass
{
public Func<TokenStream> Plumb()
{
// I'm returning a function that returns a new TokenStream for whoever calls it
return () => new TokenStream();
}
}
static void Main(string[] args)
{
var someClass = new SomeClass();
TokenStream stream = someClass.Plumb()(); // note double brackets
}
Think first-class functions in JavaScript, if it helps to grok it.
New Java brings functional interfaces, which is similar: http://java.dzone.com/articles/introduction-functional-1