Do not know C # basics-start from the similarities and differences between struct and class

Source: Internet
Author: User

 

I know that many people have discussed this issue. I don't have more than one, but I have more than one.

Some people have asked this question recently, so I want to talk about it again. The foundation is very important when the building is on a high level in the city of wanshu. If you understand it, don't be so arrogant. Please add or correct it.

Starting from origin/Definition

Struct has existed since the era of C (to Dennis. it is the abbreviation of Structure, which is the meaning of Structure. it is a basic data structure that contains one or more values or variables of the same type or different types. it is like a data storage package ".

 

Class is only available after the concept of object-oriented. It "is the blueprint for creating objects and describes the common attributes and methods of the created objects ".

 

From the usage they were created, we can see that the Class has a greater mission than Struct.

Class has been given a great mission since its birth: simulating real-world behavior, with two powerful tools: inheritance and polymorphism.

Struct has evolved into the field of C #, and it can even implement interfaces (of course, this is something not available in the C era). In this article, we will give a brief introduction.

 

Value Type VS reference type

In the. net world, System. Object supports all classes in the. NET Framework class hierarchy and provides low-level services for derived classes. This is the final base class of all classes in. NET Framework; it is the root of the type hierarchy. "All classes, structures, enumeration, and delegation are derived from the Object.

Let's take a look at the following figure to describe the two types in net. Of course, there is also a "pointer type", which we will not discuss here.

 

 

Struct belongs to the value type, and all classes are reference types.

What does this mean?

 

When we use the equal sign "assign value", for Struct, the same value is copied to another variable, and for Class, they just point their names to the same object.

 

See the following example:

Class FooClass

{

Public int FooValue;

}

Struct FooStruct

{

Public int FooValue;

}

 

 

Class Program

{

 

Static void Main (string [] args)

{

FooClass classObj = new FooClass ();

 

ClassObj. FooValue = 0;

 

FooClass classObj2 = classObj;

 

ClassObj2.FooValue = 1;

 

FooStruct structObj = new FooStruct ();

 

StructObj. FooValue = 0;

 

FooStruct structObj2 = structObj;

StructObj2.FooValue = 1;

 

}

 

}

Set a breakpoint. The value after running is as follows:

 

At the beginning, structObj2 creates a new copy and obtains all the values of structObj;

At the beginning, classObj2 only points to classObj. This is a difference between the value type and the reference type. When classObj or classObj2 is modified, the same part of memory is modified.

Struct implementation Interface

As mentioned above, Struct can implement interfaces. Here we reference an instance:

Interface IPromotion

{

 

Void promote ();

 

}

 

Struct Employee: IPromotion

{

 

Public string Name;

 

Public int JobGrade;

 

Public void promote ()

{

 

JobGrade ++;

 

}

 

Public Employee (string name, int jobGrade)

{

 

This. Name = name;

 

This. JobGrade = jobGrade;

 

}

 

Public override string ToString ()

{

 

Return string. Format ("{0} ({1})", Name, JobGrade );

 

}

 

}

 

Class Program

{

 

Static void Main (string [] args)

{

 

Employee employee = new Employee ("Cool Guy", 65 );

 

IPromotion p = employee;

 

Console. WriteLine (employee );

 

P. promote ();

 

Console. WriteLine (employee );

 

}

 

}

Once this function is added, we can define some interfaces before applying Struct so that the type of struct remains consistent in form.

 

Different purposes

Who should I choose between struct and class?

When we want to implement a certain data structure in programming, we usually choose class because of its powerful and specific mission. but when we want to pass or store some small data structures, we can consider struct.

Net framework has many defined struct, such:

System. Drawing. Rectangle

System. Drawing. Color

System. Drawing. Point

Remember the features of struct when using it.

 

This article is from ASP. NET (Alex Song) of joy. Please indicate the source

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.