K.T.
K.T.

Reputation: 83

How we can use inner anonymous for c# interface?

I am using converting one program of Java into C# in which I am having some problem of using interface anonymous. Please tell me how can I achieve this in C# This is example in java, how can we write inner anonymous for C#?

interface Test
{
    public void wish();
}

class Main
{
    public static void main(String[] args)
    {
        Test t=new Test()
        {
            public void wish()
            {
                System.out.println("output: hello how r u");
            }
        };
        t.wish();
    }
}

Upvotes: 1

Views: 156

Answers (5)

StuartLC
StuartLC

Reputation: 107247

Sadly, C# doesn't allow anonymous classes to implement interfaces in the same way Java does

i.e. The java code:

Test t = new Test(){
    public void wish(){ ...
}

Would be 'pseudo C#' equivalent to

ITest t = new /* AnonClass : */ ITest 
{
     public void wish()
     { ...
     }
}

Which of course won't compile.

As others have mentioned, you'll need to implement the interface on a named class and instantiate that, instead (Although implementing the interface directly on Main wouldn't be my first choice, either. On a nested class perhaps?).

Upvotes: 0

Patrik Hägne
Patrik Hägne

Reputation: 17151

You don't have anonymous classes the same way in C# as in Java.

The C# way to do the above would've been to use a delegate instead of an interface:

public delegate void Wisher();

class Main  
{  
    public static void main(String[] args)  
    {  
        Wisher t = () => Console.WriteLine("output: hello how r u");  
        t();
    }  
}  

Depending on your use case you might use the System.Action delegate type instead of a custom delegate type.

Upvotes: 0

Wasif Hossain
Wasif Hossain

Reputation: 3940

You have to implement the Test interface in another class say, MyTest. Then just need to instantiate the MyTest class and assign it to the instance of Test instance. See the following code:

interface Test  
{  
    void wish();  
}

class MyTest : Test
{  
    public void wish()
    {  
        System.out.println("output: hello how r u");  
    }  
}

static class Program
{  
    [STAThread]
    static void main()  
    {  
        Test t=new MyTest();  
        t.wish();  
    }  
}  

Upvotes: 1

Amit
Amit

Reputation: 15387

Try this

public interface Test  
{  
    public void wish();  
}  
class Main  : Test
{  
    public void wish(){
    //Your code
    }
}  

Upvotes: 2

Ray Poward
Ray Poward

Reputation: 366

You cannot instantiate an interface in C#. Anonymous types in C# are basically just sets of properties, so they cannot have methods defined in them and they can't implement interfaces.

You could make a class implementing an interface, have an Action field inside of it, and assign the method you want to call to that field, something like this:

using System;

public interface ITest
{
    void Wish();
}

public class Test : ITest
{
    private readonly Action _wishAction;

    public Test(Action wish)
    {
        _wishAction = wish;
    }

    public void Wish()
    {
        _wishAction();
    }
}

class Program
{ 
    public static void Main(String[] args)
    {
        Test t = new Test(() => Console.WriteLine("output: hello how r u"));
        t.Wish();
    }  
}

Alternatively, you can just use a lambda:

class Program
{ 
    public static void Main(String[] args)
    {
        Action wish = () => Console.WriteLine("output: hello how r u");
        wish();
    }  
}

Upvotes: 1

Related Questions