C #: differences between classes and structures,

Source: Internet
Author: User

C #: differences between classes and structures,

1. Reference Type and Value Type

The class belongs to the reference type, while the structure belongs to the value type.

Structure is copied when values are assigned. When a structure is assigned to a new variable, all data is copied, and any modifications made to the new copy do not change the data of the original copy.

 

2. Inheritance

The class can inherit the class or implement the interface, but the structure can only implement the interface, and cannot inherit the structure.

 

3. instantiation

The new keyword is required for class instantiation, but the new keyword is not used for Schema Instantiation.

 

4. Constructor

The class can explicitly contain constructors without parameters, but the structure cannot explicitly contain constructors without parameters. Only constructors with parameters can be defined.

 

Fifth, initialize the instance Field

Class can initialize the instance field in the class definition, but the structure is not.

In structure declaration, initialization fails unless the field is declared as const or static.

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. collections; namespace ConsoleApplication {class Program {static void Main (string [] args) {ClassPerson cp = new ClassPerson (); StructPerson sp = new StructPerson ("Mary"); cp. showName (); sp. showName (); Console. readKey () ;}// you can inherit the public struct StructPerson: IShowName {private string name; /// <summary> Name </summary> public string Name {get {return this. name;} set {this. name = value ;}// you can explicitly include the public StructPerson (string name) {this. name = name;} public void ShowName () {Console. writeLine (this. name) ;}}// you can inherit the interface and class public class ClassPerson: Creature, IShowName {// You can initialize the instance FIELD private string name = "John" in the class definition "; /// <summary> Name </summary> public string Name {get {return this. name;} set {this. name = value ;}/// the constructor without parameters can be explicitly included in the public ClassPerson (): base () {} public override void Move () {Console. writeLine ("Moving");} public void ShowName () {Console. writeLine (this. name) ;}} public interface IShowName {void ShowName () ;}public abstract class Creature {public abstract void Move ();}}

 

Pay attention to the following points:

① Method for initializing instance fields in the structure: 1. using parameterized constructors and 2. Accessing members after declaring the structure respectively. Any private member or other member that is set as inaccessible can only be initialized in the constructor.

② When you do not need to create a new structure object, you must note that before initializing all fields, the field will remain unassigned and the object will be unavailable. That is to say, before the initialization of all fields contained in the structure object (including private, public, and hidden (automatically implemented attributes generated at runtime, methods or attributes in the structure object cannot be called.

③ When the structure contains a reference type as a member, the default constructor of the member must be explicitly called; otherwise, the member will remain unassigned and the structure is unavailable.


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

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.