C # attributes of the inherited interface in the class

Source: Internet
Author: User
Tags visual studio 2010

In development, interface programming requires that attributes be stored in Interfaces. But how can I inherit the attributes of interfaces in classes?

At the beginning, I thought that attributes can be used as long as the interface is inherited. The Code is as follows:

public interface IA{        int count{get;set;}        void test();}public class A:IA{        public void test()        {             //TODO...         }}

At this time, how to compile will prompt an error, see.

Check the prompt and you will know that the attribute count in IA is not implemented. Because the attribute is essentially a method, that is, the get and set methods, but the attribute looks like a field. The Code is as follows:

  public class A : IA    {          #region count normal Property        private int count_;        public int count        {            get            {                return count_;            }            set            {                count_ = value;            }        }        #endregion        public void test()        {            //TODO...        }    }

Then you can complete the compilation.

Here is a tip: When inheriting an interface class, you can press Ctrl +. Next to the interface, and then you can quickly implement all items in the interface (including methods and attributes ). It is worth noting that there will be such code "throw new notimplementedexception ();" for methods and attributes, which requires implementation.

See Visual Studio 2010 tips for improving development efficiency

Can I add a public field to the interface so that the inherited field can also be accessed? For example:

public interface IA    {        public int sum;        int count { get; set; }        void test();    }

Compilation error. See

Fields cannot be included in the interface, so fields cannot be defined in the interface. The following is what msdn says (for more information, see this link http://msdn.microsoft.com/zh-cn/library/vstudio/ms173156.aspx ):

The interface has the following attributes:

  • Interface and abstract base class. Any option class or structure that implements the interface must implement the link of all its members.

  • The interface cannot be directly instantiated. Its members are implemented by implementing any option class or structure of the interface.

  • Interfaces can contain events, indexers, methods, and attributes.

  • The interface does not contain the implementation of methods.

  • The option class or structure can implement multiple interfaces. The option class can inherit the base class and implement one or more interfaces.

Related Article

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.