Differences between structures and classes in c #

Source: Internet
Author: User

 
Directory
Class and structure instance comparison
Differences between classes and structures
How to Select structure or Class
I. Comparison of classes and structures:
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, 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. 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.

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.