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

Source: Internet
Author: User

 

Http://www.cnblogs.com/loveyue/archive/2009/04/19/1439025.html

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 locations in the memory are 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, the operation of a variable does 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.

 

5. Differences between abstract methods and Virtual Methods Abstract METHODS
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

Bytes ---------------------------------------------------------------------------------------------

What is the difference between a virtual method and an 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

First of all, we need to 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, and the method body must be written. 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.

 

. Net divides data types into value type and reference type)

A data with value type is stored in a variable in the stack. That is, allocate memory space in the stack and store the included values directly. The values represent the data itself. Data of the value type has a fast access speed.

A data with reference type does not reside in the stack, but is stored in the heap. That is, allocate memory space in the heap. Instead of directly storing the contained values, it points to the value to be stored. The value indicates the address to which it points. When accessing a data with reference type, you need to check the content of the variable in the stack. The variable references an actual data in the heap. Data of the reference type has a larger storage scale and a lower access speed.

Value Type
(1) Where is this type allocated? Allocated on Stack
(2) How are variables represented? The value type variable is local replication.
(3) What is the base type? Must inherit from system. valuetype
(4) can this type be used as the base class of other types? No. The value type is sealed and cannot be inherited.
(5) What is the default parameter transfer? Variable is passed by value (that is, a copy of the variable is passed into the called function)
(6) can this type rewrite system. Object. Finalize. The value type cannot be placed on the stack, so it does not need to be terminated.
(7) Can I define constructors for this type? Yes, but the default constructor is retained (that is, the User-Defined constructor must have all parameters)
(8) When will this type of variable die? When they reach a defined scope

Cited type
(1) Where is this type allocated? Allocated on managed stacks
(2) How are variables represented? The reference type variable points to the memory occupied by the allocated instance.
(3) What is the base type? It can inherit from any type except system. valuetype, as long as the type is not sealed's
(4) can this type be used as the base class of other types? Yes. If this type is not sealed, it can be used as a base class of other types.
(5) What is the default parameter transfer? Variables are passed by reference (for example, the address of the variable is passed into the called function)
(6) can this type rewrite system. Object. Finalize () or indirectly?
(7) Can I define constructors for this type? Of course!
(8) When will this type of variable die? When the managed heap is reclaimed

 

 

The difference between the value type and the reference type is that when function parameters are passed.
The Value Type copies its own value and passes it to other function operations. No matter how the copied value is changed, its own value will not change.
The reference type is to pass its memory address to other function operations. The operation is to reference the type value itself, so the value is changed by the function.
This is the difference between data transfer and data transfer.

 

 

Http://www.cnblogs.com/loveyue/archive/2009/04/19/1439025.html

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 locations in the memory are 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, the operation of a variable does 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.

 

5. Differences between abstract methods and Virtual Methods Abstract METHODS
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

Bytes ---------------------------------------------------------------------------------------------

What is the difference between a virtual method and an 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

First of all, we need to 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, and the method body must be written. 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.

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.