Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between C # structure and class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the differences between C# structure and class". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what are the differences between C# structure and class"?

1. The C# structure is a stack object and cannot be created on the heap

2. C# structures can inherit interfaces, but not from other structures

3. The default constructor of the structure cannot be overridden. When you need a custom constructor, you must create a constructor with parameters to ensure that the parameter list of the constructor is different from that of the default constructor.

4. When using new to create a structure object, its constructor is automatically called. If you only declare a structure type variable like an int type variable without new, then the member variables in the structure must be initialized before you can use the structure object.

Example of C# structure:

Struct Student: IGrade public int maths; public int english; public int csharp; public int GetTot () {return maths+english+csharp;} public Student (int y) {maths = english = csharp = y;}} public string GetGrade () {if (GetTot () > 240) return "Brilliant"; if (GetTot () > 140) return "Passed"; return "Failed";} interface IGrade {string GetGrade ();}

Let's take a look at how to use the C# structure you just created. The code is as follows:

Student S1 = new Student (); Console.WriteLine (s1.GetTot ()); Console.WriteLine (s1.GetGrade ())

Output of C# structure example:

0 Failed

The above code shows that the default constructor is called, and the constructor automatically initializes all int member variables in the structure to 0, which is why the sum is 0.

Here's an interesting piece of code:

Student S2; s2.maths = s2.english = s2.csharp = 50; Console.WriteLine (s2.GetTot ()); Console.WriteLine (s2.GetGrade ())

Output of C# structure example:

150 Passed

In the above code, we do not use new to create the structure, so the constructor will not be called, just declare a variable S2 of the structure type, but then we initialize the assignment to all member variables of the structure, then the structure can still be used. Although many people argue that this assignment trick is too unprofessional and silly, no one can find a reasonable explanation, but it is feasible. When we comment out the assignment statement, the compiler reports an error: an unassigned local variable S2 is used.

We can also use a custom constructor and pass an integer variable to create a structure object, as follows:

Student S3 = new Student (90); Console.WriteLine (s3.GetTot ()); Console.WriteLine (s3.GetGrade ())

Output of C# structure example:

270 Brilliant

Unlike a class is a reference type, the structure is a value type, so the structure is easier to operate than a class. When using classes to store only some data, you will find that using structures is much better! Structured arrays are created on the heap, and to create objects in the form of a class, memory must be allocated on the heap in advance, and the reference address of each class needs to be saved additionally, so using structured arrays is more efficient. In fact, most of the classifications are structures in the .net framework, such as System.Drawing.Point.

Thank you for your reading, the above is the content of "what are the differences between C# structure and class". After the study of this article, I believe you have a deeper understanding of the differences between C# structure and class. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report