Interface comparisons in Java and C # (something you don't know)

Source: Internet
Author: User

1. Unlike Java, interfaces in C # cannot contain fields (field).
In Java, an interface can contain fields, but the characters are static and final De Yin. While C # does not allow fields in the interface, the compiler will prompt for errors at compile time (as shown in the following code).
Interface IInterface            
    {        int A;//Compiler Error Hint: interface cannot contain fields    }
2. In C # and Java, all methods within an interface are common methods by default.
Interface IInterface    {public        int add ();//Error prompt: Modifier "Public" is not valid for this item    }
3, C # Unique implementation of the interface and explicit implementation of the interface.
Common Implementation Interface
Interface IInterface    {        int add (int a,int b);    }    Class Program:iinterface    {        static void Main (string[] args)        {program            II = new program ();     Use entity classes to access your own interface functions            Console.WriteLine (Ii.add (1, 2));//run correctly without prompting error        } public        int Add (int a, int b)        {            return a + b;        }    }
Display to Implement Interface
Interface IInterface    {        int add (int a,int b);//Compiler Error Hint: interface cannot contain field    }    class Program:iinterface    {        static void Main (string[] args)        {program            Newprogram = new program ();            Newprogram.add (1, 2);//Compiler Prompt Error: "Consoleapplication3.program" does not contain the definition of "add",                                 //and cannot find an acceptable type of " Consoleapplication3.program "The first parameter of the extension method" add "(Is it missing a using directive or assembly reference?)                  }        int Iinterface.add (int a, int b)//Display Implementation interface, note that this method is more Iiterface qualifier        {            return a + b;        }    }

Also, we notice that the public modifier is missing in int iinterface.add (int a, int b) after explicitly implementing the interface, but we add the compiler to the error: "Modifier" public "is not valid for this item." Also, because the program class accesses its own method, accessibility is not the reason why the Add () method cannot be accessed.

If we modify the code in the main () function, assign the variable of the new program () to the Iiterface reference, and see how the result

static void Main (string[] args)        {            IInterface II = new program ();            Console.WriteLine (Ii.add (1, 2));//correct operation, no prompt error        }

From the above results, we can see that if the display implementation interface, then the docking port method access must be through the interface type of reference, with the display implementation of the interface class is no way to use.

Two main uses of explicit interface member implementations

· Because explicit interface member implementations cannot be accessed through classes or struct instances, they do not belong to the public interface of the class or struct itself. This is especially useful when you need to implement some interface in a common class or struct that is for internal use only (no outside access is allowed).

· Explicit interface member implementations eliminate the ambiguity caused by an interface member that contains multiple identical signatures at the same time. Without an explicit interface member implementation, it is not possible for a class or struct to provide an implementation for an interface member with the same signature and return type individually, or for any of the interface members with the same signature and different return types.

Interface comparisons in Java and C # (something you don't know)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.