Class and struct comparison, struct comparison

Source: Internet
Author: User

Class and struct comparison, struct comparison

// Structure example: public struct Person {string Name; int height; int weight} public bool overWeight () {// implement something} class Example: public class TestTime {int hours; int minutes; int seconds;} public void passtime () {// implementation of behavior} call process: public class Test {public static ovid Main {Person Myperson = new Person // declaration structure TestTime Mytime = New TestTime // Declaration class }}

From the above example, we can see that the declaration of the class and the declaration of the structure are very similar, but the difference between struct and class is after the qualifier, and when used, the methods for defining new structures and new classes are similar.

I. Differences between classes and structures
1. Value Type and reference type
Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to System. int32 structure. string corresponds to system. string Structure. You can create more value types by using the structure.
Class is a reference type: the reference type is allocated on the stack.
The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.
Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference.
Note:
A. Although the structure is different from the class type, their base types are all objects. in c #, all types of base types are objects.
B. Although the New operator is used to initialize the instance field of the structure, the structure object is still allocated to the stack instead of the stack,
If "new" (new) is not used, the field remains unassigned before all fields are initialized, and the object is unavailable at this time.

If new is used for initialization, the default value is assigned to the variable. That is, if Myperson. height is called, the default value is 0. If new is not used, this call will not work.

2. Inheritance
Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed.
Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.
Note: although the structure cannot be inherited, the structure can inherit interfaces, and methods are the same as class inheritance interfaces.


3. Internal Structure:
Structure:
No default constructor exists, but you can add constructor.
No destructor
No abstract and sealed (because it cannot be inherited)
The protected modifier cannot exist.
You do not need to use new for initialization.
It is incorrect to initialize the instance field in the structure.
That is to say, no
Struct xx
{
Public int testint = 1; // The instance field initialized in the structure is incorrect.
Public static int testint = 1 // This is acceptable. testint is not an instance field, but a static field.
}
The default (No parameter) constructor of the declared structure is incorrect. If you want to write constructor, this function should initialize the structure members as their default values.
For example:
Struct xx
{
Public int testint;
Public xx (string yy)
{
Testint = yy;
}
}
// The following is incorrect (an empty non-parameter constructor cannot be defined)
Struct xx
{
Public int testint;
Public xx ()
{
}
}


Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
You must use new to initialize instance fields and function methods.


Differences between C/C ++ classes and struct

The difference is that the default access label is different: If no access label exists before the definition of a member in the class, the member is a private member by default in the class defined by the class keyword; in a class defined by the struct keyword, this member is public by default.
References: C ++ primer

Similarities and differences between classes and struct types

In C ++, there are only two differences:
(1) The default access permission for members in class is private, while that for struct is public.
(2) inherit from class is private by default, while inherit from struct is public by default.
There is no difference.

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.