Structure struct class (post) Notes

Source: Internet
Author: User
    1. Structure and class
      The class is the reference type, and the structure is the value type.
    2. Heap or stack?
      When the new operator is called on the class, it is allocated on the stack. However, when the structure is instantiated, the structure will be created on the stack. This will generate performance gains. In addition, you will not process the reference to the structure instance as you do with the class. You will operate on the Structure instance directly. For this reason, when a structure is passed to a method, the structure is passed through a value instead of as a reference.
    3. Constructor and inheritance

      The structure can declare constructors, but they must contain parameters.The default (No parameter) constructor of the declared structure is incorrect.The structure member cannot have an initial value. The default constructor is always provided to initialize the structure members as their default values.

      When you use the new operator to create a structure object, this structure object is created and an appropriate constructor is called.Different from the class, the new operator can be used for structure instantiation.If "new" (new) is not used, the field remains unassigned before all fields are initialized, and the object is unavailable.

      ForStructure. It does not exist as a class.A structure cannot inherit from another structure or class, and cannot be the base of a class. However, the structure inherits from the base class object. The structure can implement interfaces, and the implementation method is exactly the same as that of class implementation interfaces.

Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Cappstruct
{
/**/ /// <Summary>
///Myx. Name
/// </Summary>
Class Program
{
Static   Void Main ( String [] ARGs)
{
// Struct
Scontact CT =   New Scontact ();
Ct. m_name =   " Mike " ;
Ct. m_age =   25 ;
Ct. m_tel =   " 123-456-789 " ;
Scontact CT2 = CT;
Ct2.m _ name =   " Roger " ;
Ct2.m _ age =   23 ;
Ct2.m _ Tel =   " 789-123-456 " ;
Console. writeline ( " After modifying the structure variable: " );
Console. writeline ( " Name: "   + Ct. m_name );
Console. writeline ( " Age: "   + Ct. m_age.tostring ());
Console. writeline ( " Tel: "   + Ct. m_tel );
// Class
CContact CC =   New CContact ();
Cc. m_name =   " Mike " ;
Cc. m_age =   25 ;
Cc. m_tel =   " 123-456-789 " ;
CContact CC2 = CC;
Cc2.m _ name =   " Roger " ;
Cc2.m _ age =   23 ;
Cc2.m _ Tel =   " 789-123-456 " ;
Console. writeline ( " After modifying the class variables: " );
Console. writeline ( " Name: "   + Cc. m_name );
Console. writeline ( " Age: "   + Cc. m_age.tostring ());
Console. writeline ( " Tel: "   + Cc. m_tel );
Console. writeline ( " Press any key to exit ...... " );
Console. readkey ();
}
}

Struct Scontact
{
Public StringM_name;
Public IntM_age;
Public StringM_tel;
}

Class CContact
{
Public   String M_name;
Public   Int M_age;
Public   String M_tel =   " Unknown " ;
}

}

Running result:

After modifying the Structure Variable
Name: Mike
Age: 25
Tel: 123-456-789
After modifying the class variable
Name: Roger
Age: 23
Tel: 789-123-456

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.