C # differences between classes and structures in basic series-C #

Source: Internet
Author: User
Differences between classes and structures
1. Value Type and reference type

Structure: The structure is a value type;

Value types are allocated addresses 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: the class is a reference type;

The address of 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 treated as a base type, and classes process a business logic.

Assign values. Because the structure is a value type, you can create a new structure for the assignment between structures, while the class is a reference type, and the assignment between classes is just a copy reference.

Note:

Although the structure of u is different from the class type, their base types are all objects, and all types of base types in C # Are objects;

Although the new operator is used for structure initialization, the structure object is still allocated to the stack rather than the stack;

U structure declaration:

Person Myperson = new Person () // declaration Structure
Person Myperson;

Use the new key to create a structure, call the structure constructor, and initialize all fields (each field obtains the default value based on the declared type );

If new is not used, the field remains unassigned and the object is unavailable. All data members must be initialized before the object can be used. attributes and methods cannot be used for initialization because no function member can be called before all data members are initialized, the data member must be declared as public.

Note: When struct has attributes, you must use new to create struct variables.

For example, the following code will cause an error. The unassigned Variable p is used.

Public struct Person

{

PRivate int age;

Public int Age

{

Get {return age ;}

Set {age = value ;}

}

}

Class Program

{

Static void Main (string [] args)

{

Person p;

P. Age = 10;

}

}

Correct code:

Public struct Person

{

Private int age;

Public int Age

{

Get {return age ;}

Set {age = value ;}

}

Public Person (int age)

{

This. age = age;

}

}

Class Program

{

Static void Main (string [] args)

{

Person p = new Person (10 );

}

}

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 from other classes and interfaces and inherit from itself.

Note:

Although the structure cannot be inherited, the structure can inherit interfaces. methods are the same as class inheritance interfaces.

Example: structure implementation Interface

Interface IImage
{
Void Paint ();
}

Struct Picture: IImage
{
Public void Paint ()
{
// Painting code goes here
}

Private int x, y, z;
// Other struct members
}

3. Internal Structure

Structure:

U does not have a default constructor, but you can add constructor.

U has no destructor

U does not have abstract and sealed (because it cannot be inherited)

U cannot have a protected modifier (the default modifier for structure members is private, the same as the class)

U can be initialized without using new

U initializing the instance field in the structure is incorrect.

Class:

U has default constructor

U destructor

U can use abstract and sealed

U has the protected Modifier

U must be initialized using new

U can initialize instance fields in the class

3. How to Select a class or structure
The structure can be seen as a lightweight class, which is better in performance.

For a large number of logical objects, creating a class is better than creating a structure.

The u structure represents 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.

U is the best choice for classes when presenting abstract and multi-level object layers.
 
U in most cases, this type is only the best choice for structure when it comes to some data.

Transferred from

Http://www.knowsky.com/539298.html

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.