C # Class and structure difference

xiaoxiao2021-04-02  269

C # Structure and Class Differences (Summary of others)

How to select the structure or class one. Class and structure example comparison: Structure Example: Public Struct Person {String Name; INT Height; int Weight public bool overweight () {// Implement Something}} Sample: public class testtime {int.com; int minutes; int 2 Public void passtime () {// importation of behavior}} calling process: public class test {public static ovid main {PERSON MyPerson = New Person // Declaration Structure TestTime MyTime = New TestTime // Declaration class}} From above We can see that the statement of the statement and structure of the class is very similar, but the definition is Struct or the difference between the Class, and when using, the new structure and the new type of method are also very similar. So what is the specific difference between classes and structures? II. Differences of class and structures 1. Value type and reference type structure are value type: Value type assigning addresses on the stack, all base types are structural types, such as: int Cord corresponds to System.String, STRING Structure, by using the structure, more value type classes are categories: Reference Types The execution efficiency of the assigned address stack on the pile is higher than the execution efficiency of the stack, but the resource of the stack is limited, not suitable for processing large logic complex Object.

Therefore, structural processing as a small object treated by the base type, and the class handles a business logic because the structure is the value type, and the assignment between the structure can create a new structure, and the class is a reference type, the assignment between the classes is just a replication reference note. : 1. Although the structure is different from the type of class, their base type is object (Object), all types of base types in C # are Object 2. Although the structure is initialized, the new operator is still structural objects. Allocated on the stack instead of the pile, if you do not use "New", the field will remain unfaised before initializing all fields, and the object is not available 2. Inheritability: Cannot inherit from another structure or class, itself cannot be inherited, although the structure is not clearly declared, but the structure is implicit SeaD. Class: Fully scalable, unless the displayed declaration Sealed Other classes and interfaces can be inherited, and it can also be inherited. Although the structure cannot be inherited, the structure can inherit the interface, the method and class inheritors, for example: structure implement interface interface IIMAGE {void Pict ();} struct picture: iimage { Public void Paint () {// Painting Code Goes Here} Private Int X, Y, Z; // Other struct members} 3. Internal Structure: Structure: There is no default constructor, but you can add constructor without the destructor without Abstract and SeaRed (because you can't inherit) You can have a protected modifier that can be initialized in the structure. The instance field is incorrect: There is a default constructor with a destructor that can use Abstract and Sealed with protected modifiers must be initialized using New. How to choose a structure or class to discuss the structure of the structure and class and the difference, the following discusses how to choose the use structure or class: 1. The stack has limited space, for a large number of logical objects, creating classes to be better than the creation structure 2. The structure represents a lightweight object such as points, rectangles, and colors, for example, if a array containing 1000 point objects is declared, the additional memory is assigned to each object. In this case, the cost of the structure is low. 3. When the abstract and multi-level object hierarchy, the class is the best choice 4. In most cases, the type is just some data, the best choice is the following part of the following sections are based on the instance provided by the Microsoft website.

Class library: struct2.cs

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls. WebParts; using system.web.ui.htmlcontrols;

///

/// struct2 Summary Description /// Distinguisimetricity of the demonstration class and structure /// public class structure {

public Struct2 () {// // TODO: Add constructor logic here //}} public class TheClass {public int X;} public struct TheStruct {public int Y;} public class TestClass {public int structtaker (TheStruct s ) {RETURN SY = 5; // Getting initial value} public int classTaker (theclass c) {return cx = 5; // assigning initial value}} INVOKE section: Button event code

protected void Button2_Click (object sender, EventArgs e) {TheStruct a = new TheStruct (); TheClass b = new TheClass (); aY = int.Parse (TextBox1.Text); bX = int.Parse (TextBox1.Text); TestClass T = new testclass (); t.StructTaker (a); t.classtaker (b); label1.text = "a;" aytoString () "..." "b:" bxtostring () ;

}

operation result:

The value in TextBox1 is 5, A = 5 b = 5

When the value in the textbox1 is not a first value 5, a = (value in TextBox1) B = 5

转载请注明原文地址:https://www.9cbs.com/read-131344.html

New Post(0)