C # class and Interface Definition

Source: Internet
Author: User
C # class definition C # Use the class keyword to define the class: Class myclass {// class members.} by default, the class is declared as internal, that is, only Code You can also use the internal keyword to explicitly declare: internal class myclass {// class members .} you can specify that the class is public, that is, the code in other projects can be used to access the declaration with the public Keyword: public class myclass {// class members .} you can also specify the class as an abstract class: it cannot be instantiated, it can only inherit, it can have abstract members, use the keyword abstract to seal the class: it cannot inherit, and use the keyword sealed to declare the abstract class: public abstract class myclass {// class members, may be abstract .} the abstract class can be public or internal declared and sealed class: Public sealed class myclass {// class members .} the abstract class can be public or internal, and the inheritance can be specified in the class definition. Is the class name followed by a colon plus the base class name: public class myclass: mybase {// class members .} note: Only one base class can be found in C #. If an abstract class is inherited, all the abstract members inherited must be executed (unless the derived class is also abstract) the compiler does not allow a derived class to have higher accessibility than its base class. That is, the internal class can inherit from a public class, but the public class cannot inherit from an internal class. If no base class is specified, the defined class is inherited from system. objects can also specify supported interfaces after the colon. If a base class is specified, the colon follows the base class name, and then one or more specified interfaces are separated by commas. For example: specify an interface for myclass: public class myclass: imyinterface {// class members .} specify the base class and interface for myclass: public class myclass: mybase, imyinterface {// class members.} Multiple specified interfaces: public class myclass: mybase, imyinterface, and imysecondinterface {// class members .} access modifier combination: modifier meaning none or internal class can only access public class in current project can access abstract or internal abstract class anywhere can only access in current project, cannot be instantiated, only the public abstract class can be inherited and can be accessed anywhere. It cannot be instantiated. It can only inherit the sealed or internal sealed class and can only be accessed in the current project. It cannot be derived. Only the public sealed class can be instantiated and accessed anywhere, it cannot be derived. The definition of the instantiated interface can only be similar to that of the class declaration. Use the interface Keyword: interface imyinterface {// interface members .} modify Public interface imyinterface {// interface members .} abstract and sealed keywords cannot be used in Interfaces. Because interfaces do not contain Execution Code, they cannot be directly instantiated. They must inherit from similar interfaces and classes. However, they can use multiple basic interfaces: public interface imyinterface: imybaseinterface, imybaseinterface2 {// interface members .} the interface is not a class, not from system. object Inheritance causes the system. object members can use these interfaces system. object common constructor object initializes a new instance of the object class. The public method equals has been overloaded. Determine whether the two object instances are equal. Gethashcode is used as a hash function of a specific type. GetType: Get the type of the current instance. Referenceequals determines whether the specified object instance is the same. Returns the string of the current object. The protected method finalize destructor memberwiseclone creates a superficial copy of the current object.

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.