The difference between structure and class in C #

Source: Internet
Author: User
Tags abstract comparison constructor modifiers reference
Difference Directory
Comparison of instances between classes and structs
The difference between class and structure
How to select a structure or class

A Example comparison of classes and structs:
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 Procedure:
public class Test
{
public static Ovid Main
{
Person Myperson=new person//Declaration structure
Testtime mytime=new testtime//Declaration class
}
}
As we can see from the example above, the declaration of a class is very similar to that of a struct, except that the qualifier is the difference between struct and class, and the method of defining a new structure and defining a new class is very similar when used. So what is the specific difference between a class and a structure?

Two. Differences in class and structure
1. Value type and reference type
Structs are value types: value types assign addresses on the stack, all base types are struct types, for example: int corresponds to SYSTEM.INT32 structure, string corresponds to System.String structure, you can create more value types by using structs
Class is a reference type: A reference type assigns an address on the heap
The execution efficiency of the stack is higher than that of the heap, but the stack has limited resources and is not suitable for dealing with large logically complex objects. So the struct handles a small object treated as a base type, while a class handles a business logic
Because structs are value types, assignments between structs can create new structures, while classes are reference types, and assignments between classes are just copies of references
Note:
1. Although structs are not the same as class types, their base types are objects (object), and all types in C # are of the base type
2. Although the initialization of the structure also uses the new operator, the structure object is still allocated on the stack rather than on the heap, and if you do not use new, the field remains unassigned and the object is unavailable until all the fields are initialized.
2. Inheritance of
Structure: cannot inherit from another structure or class, nor can it be inherited, although the structure is not explicitly declared with sealed, but the structure is implicit sealed.
Class: Fully extensible unless the declared declaration sealed otherwise the class can inherit other classes and interfaces and it can be inherited
Note: Although structs cannot be inherited, structs can inherit interfaces, and methods and classes inherit interfaces
For 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:
There is no default constructor, but you can add constructors
No destructor
No abstract and sealed (because it cannot be inherited)
Cannot have protected modifiers
You can initialize it without using new
Initializing an instance field in a struct is an error
Class:
There is a default constructor
There is a destructor
You can use abstract and sealed
There are protected modifiers
You must use new to initialize

Three How to select a structure or class
After discussing the similarities and differences between structs and classes, the following discusses how to choose a structure or a class:
1. The stack has limited space, and for a large number of logical objects, creating a class is better than creating a struct
2. Structs represent lightweight objects such as dots, rectangles, and colors, for example, if you declare an array that contains 1000-point objects, additional memory is allocated for referencing each object. In this case, the cost of the structure is lower.
3. Classes are the best choice when it comes to expressing abstractions and multi-level levels of objects
4. In most cases the type is just some data when the structure is the best choice

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.