0

I am trying to write some equivalent C# code to the following Java one:

public class XLexer extends antlr.CharScanner implements TokenStream {
protected int stringCtorState = 0;

public String mString() { return ""; }

public Token nextToken()  {
       resetText(); // exists in CharScanner class
       return null; // will return something
}

public TokenStream plumb() {
    return new TokenStream() {
        public Token nextToken()  {
            resetText(); // exists in CharScanner class
            if (stringCtorState >= 0) { String x = mString(); }
            return null; // will return something
        }
    };
}

}

Can anyone give me an hint how to do it in C# because I am getting errors when defining the method nextToken inside the return statement.

thanks!

1
  • 1
    Lookup delegates in C#, they are the way to go in this language. Commented Jan 15, 2014 at 12:37

3 Answers 3

4

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:

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

Sign up to request clarification or add additional context in comments.

8 Comments

Actually there are (return new { Prop1 = 1, Prop2 = 2}) but "anonymous" has a very different meaning in C# and Java. This kind of declaration in C# may be done with a delegate or (much better) respecting original intent with a private class and a field of base class type.
@Konrad Thanks for your detailed answer. My issue now is that nextToken method uses variables and methods defined in the outer class. how is that achieved in C#?
No problem - the anonymous method can see them, it is created within the class after all. If it is more than a one-liner, just enwrap its body with curly brackets (return () => { something; something; return new TokenStream();}, the shortened notation I used in my example is kind of syntax sugar. If you still have troubles, edit your answer and post the actual code, that'd probably be the quickest way to solve the issue
Hi again. I am not being able to get my code running. I am not being able to access the outer methods and variables. I have a class called XLexer that is automatically generated but I can add customized code (is where I am trying to add the code I previously referred). This XLexer class implements the class TokenStream and has a method called mString() - this is automatically generated. So, when I try to implement my custom code referred in the message I get an error saying that mString() can only be accessed if static.. any hint @KonradMorawski?
@pinker depending on your intentions, I guess you either need to implement mString on your anonymous TokenStream instance, or to prefix mString like that: XLexer.this.mString
|
1

Use delegates instead. An example here: http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx

Comments

1

Not sure if this is your desired result.

but the way I see it is you just want to return a TokenStream object which has the method of nextToken which returns an object Token

public class TokenStream
{
    public Token nextToken()
    {
         return new Token();
    }
}

that would be your TokenStream class and then you could have a method/function else where which does the following

public TokenStream plumb()
{
    return new TokenStream();
}

the usage of which would be

TokenStream ts = plumb();
Token t = ts.nextToken();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.