1

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();
    }
}
firedev
  • 20,898
  • 20
  • 64
  • 94
K.T.
  • 83
  • 1
  • 11
  • Only for info. Usually Interfaces starts with letter I, in this example ITest. It´s a good way to know which ones are interfaces. Amit answered your question pretty well! ;) – Oscar Bralo Feb 20 '14 at 07:43
  • possible duplicate of [Creating an instance of a interface in c#](http://stackoverflow.com/questions/17708528/creating-an-instance-of-a-interface-in-c-sharp) – Fried Feb 20 '14 at 08:02
  • possible duplicate of [Is there a C# equivalent of this?](http://stackoverflow.com/questions/15581737/is-there-a-c-sharp-equivalent-of-this) – nawfal Jun 28 '14 at 18:41

5 Answers5

2

Try this

public interface Test  
{  
    public void wish();  
}  
class Main  : Test
{  
    public void wish(){
    //Your code
    }
}  
Gusdor
  • 14,001
  • 2
  • 52
  • 64
Amit
  • 15,217
  • 8
  • 46
  • 68
1

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();
    }  
}
Ray Poward
  • 366
  • 5
  • 12
1

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();  
    }  
}  
Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
0

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.

Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
0

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?).

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • sorry but any of the solution above mentioned won't work as there are diff implementation for wish method in different classes when I implement the interface "Test". Any way I am trying to work out this with delegates or lambda expressions. – K.T. Feb 25 '14 at 07:21
  • 1
    Yes, so each different anonymous class implementation would need to have its own (named) class which implements `ITest`. If wish is the ONLY method on the interface, and if the anonymous class doesn't require state in between calls to the method, as you say, a Lambda implementation would be a good conversion. – StuartLC Feb 25 '14 at 07:47