Differences between structures and classes in C #

Source: Internet
Author: User

I. Comparison of classes and structures:
Structure example:

 1 public struct Person  2 {  3 string Name;  4 int height;  5 int weight  6 public bool overWeight()  7 {  8 //implement something  9 } 10 } 

Class Example:

 1 public class TestTime  2 {  3 int hours;  4 int minutes;  5 int seconds;  6  7 public void passtime()  8 {  9 //implementation of behavior 10 } 11 } 

Call process:

1 public class test 2 {3 Public static Ovid main 4 {5 person myperson = new person // declaration structure 6 testtime mytime = new testtime // Declaration class 7} 8}

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, similar methods are used to define new structures and new classes. What are the specific differences between classes and structures?

Ii. Differences between classes and structures

1. the value type and reference type structure are value types: value types are allocated addresses on the stack, and 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. The class is a reference type. The allocation of address stack on the stack of the reference type is more efficient than that on the stack, 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, you can create a new structure by assigning values between structures. Classes are reference types, and assigning values between classes is just copying references. Note:
Although the structure is different from the class type, their base types are all objects. in C #, all types of base types are objects.
Although the new operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable.

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. The class can inherit other classes and interfaces unless the declared sealed is displayed. Note: although the structure cannot be inherited, the structure can inherit interfaces, the method is the same as the class inheritance interface.

Example: structure implementation Interface

 1 interface IImage 2 { 3     void Paint(); 4 } 5  6 struct Picture : IImage 7 { 8     public void Paint() 9 {10     // painting code goes here11 }12 private int x, y, z; // other struct members13 }

3. Internal Structure:

Structure: no default constructor, but you can add constructor. No destructor, no abstract and sealed (because it cannot be inherited) there cannot be a protected modifier. It is an incorrect class to initialize the instance field in the structure without using new initialization:
Default constructor and destructor. abstract and sealed must be initialized with the protected modifier.

3. How to Select a structure or a class after discussing the similarities and differences between the structure and the class, the following describes how to choose a structure or a class:

1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.

2. structure indicates lightweight objects such as dots, rectangles, and colors. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. In this case, the structure cost is low.

3. Classes are the best choice for presentation of abstract and multi-level object Layers

4. In most cases, this type is the best choice for structure when only some data is used.

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.