C # differences between class and interface, virtual method, and abstract method, value type, and reference type

Source: Internet
Author: User

This article from: http://113620916.qzone.qq.com/blog/1209003303

C # differences between class and interface, virtual method, and abstract method, value type, and reference type

1. C # differences between classes and interfaces
The interface is responsible for defining functions. The project uses interfaces to define the concepts of classes, Operation classes, and abstract classes!
The class is responsible for the specific implementation of the function!
There are also abstract class definitions in the class. The difference between the abstract class and the interface is:
An abstract class is an incomplete class. The class contains abstract methods, attributes, and specific methods and attributes, which requires further specialization.
But the interface is a behavior specification, and everything in it is abstract!
A class can only inherit one base class, that is, the parent class, but multiple interfaces can be implemented.
PS: In addition to standardizing a behavior, interfaces play an important role in a specific project. In the use of object-oriented design principles and design patterns, all of them reflect the benefits of using an interface. The most direct thing is the OCP (open and closed principle) in the design principle. We use interfaces without worrying about their specific implementations, the detailed changes in implementation have nothing to do with the use of the client (the class using the interface). It is open to extensions. We can also write an interface implementation to expand the current program, it does not affect the upper-layer usage, but it is closed to the modification. That is, we cannot modify the interface definition. Of course, this "cannot" means that we should not do this in principle!

2. Differences between abstract classes and interfaces
A:
Abstract class can contain function definitions and implementations. interfaces can only contain function definitions.
Abstract classes are abstract concepts from a series of related objects, so they reflect the internal commonalities of things. interfaces are a functional Convention defined to satisfy external calls, therefore, it reflects the external characteristics of things.
Analyzes objects and extracts internal commonalities to form abstract classes to indicate the object nature, that is, "What"
Interfaces are preferred when external calls or functions need to be expanded

3. C # What is the difference between the value type and the reference type in the language?

Answer
The difference between the value type and the reference type is that the value type variables directly store the actual data, while the reference type variables store the data address, that is, the object reference.
Value Type variables directly store the value of the variable in the stack. variables of the reference type store the actual data address in the stack, while actual data is saved in the heap. Note that heap and stack are two different concepts, and the storage location in the memory is also different. heap is generally used to store variable-length data, such as string type; the stack is used to store data of a fixed length, for example, INT (each int variable occupies four bytes ). It can be learned from the data storage location that when one value variable is assigned to another value variable, two identical values will be saved in the stack; if you assign a reference variable to another reference variable, two references to the same heap location are saved in the stack, that is, the address of the same heap is saved in the stack. When performing data operations, for the value type, because each variable has its own value, operations on a variable will not affect other variables; for variables of the reference type, to operate on the data of a variable is to operate on the data of this variable in the heap. If two variables of the reference type reference the same object, the actual meaning is that they store the same heap address in the stack, so the operation on a variable will affect another variable that references the same object.

4. Differences between structures and Classes

Answer
1) The structure is a value type stored on the stack, and the class is a reference type, stored on a controlled stack.
2) perform operations on the data in the structure to compare the data in the class or object quickly.
3) The structure is generally used to store multiple types of data. When creating a small object shared by many classes or objects, the structure efficiency is higher.
4. Differences between abstract and Virtual Methods
Abstract Method
Use the abstract keyword public abstract bool withdraw (...);
An abstract method is a method that must be overwritten by a derived class.
Abstract methods can be viewed as virtual methods without implementing the body.
If a class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods

Virtual Method
Use the virtual keyword Public Virtual bool withdraw (...);
When a virtual method is called, The runtime determines the instance of the class of the called object and calls an appropriate overwriting method.
Virtual methods can have implementations

What is the difference between the dummy virtual method and the abstract method?

Abstract methods are implemented only when declarations are not implemented, and must be implemented in subclasses. Virtual methods are sound and implemented, and can be overwritten in subclasses, or the default Implementation of parent classes can be used without overwriting.

Implementation Code for Virtual Methods

No abstract method,

Abstract classes cannot be instantiated. They can only be instantiated to implement the derived classes of all abstract methods.

An abstract method is a virtual method.
The abstract method is not implemented. Its existence is only a unified interface for the derived class. The derived class should implement this method.
If you write a base class, it will never be implemented, you should define one or more methods in this class
Abstract method.

Abstract methods are implemented only when declarations are not implemented, and must be implemented in subclasses. Virtual methods are sound and implemented, and can be overwritten in subclasses, or the default Implementation of parent classes can be used without overwriting.

Add one point

Only abstract methods can be declared in abstract classes.

Learning

In fact, you must first understand that the virtual method is closely related to polymorphism. The virtual method allows the derived class to completely or partially rewrite the method of this class. You need to write the method body. Abstract classes can contain abstract methods and general methods. abstract classes cannot be new. abstract methods are just a definition and there is no method body, that is, there is no {}. Do not write content in them. Both of them are similar in that they are overwritten.

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.