The first structure is the value type.
Structs are defined using the struct keyword, and the structure is as follows:
struct structure name
{
}
Structure Overview
The structure has the following characteristics:
A struct is a value type, and a class is a reference type. (A struct cannot contain an explicit parameterless constructor)
Unlike classes, structs can be instantiated without the use of the new operator.
Structs can declare constructors, but they must take parameters.
A struct cannot inherit from another struct or class and cannot be used as a base for a class. All structures are directly inherited from System.ValueType, which inherit from System.Object.
Structures can implement interfaces.
Structs cannot be given an initial value when defining a variable. Plus
If you want to use a constructor in a struct, you must assign a value to all variables (assign a value to the variable directly in the constructor instead of assigning a value to the variable's property, because the property has no value before it is assigned, so it cannot be given directly to the property) (plus)
All structures are implicitly inherited from the ValueType class and do not need to display the specified;. Plus
Only interfaces are allowed in the inheritance list of structs. Plus
The inheritance of the structure is three layers: Object >> valuetype >> "Structure". Plus
Example: (self-written, description of employee information, name, age, salary)
Structure, which is a value type
Structure cannot be given an initial value when defining a variable
struct EMPLOYEESTRUCT
{
private string name;
public string Name
{
get {return name;}
set {name = value;}
}
private int age;
public int Age
{
get {return age;}
set {age = value;}
}
private int Gongzhi;
public int Gongzhi
{
get {return Gongzhi;}
set {Gongzhi = value;}
}
Parametric constructors
Public Employeestruct (string _name, int _age, int _gongzhi)
{
If you want to use constructors in structs, you must assign values to all variables (assign values in constructors)
THIS.name = _name;
This.age = _age;
This.gongzhi = _gongzhi;
}
}
Working with structures
Class Program
{
static void Main (string[] args)
{
Instantiate the EMPLOYEESTRUCT structure
Employeestruct empstruct = new Employeestruct ("Steven", 22, 10000);
Console.WriteLine ("Name: {0}\n Age: {1}\n salary: {2:c2}", Empstruct. Name, Empstruct. Age, Empstruct. Gongzhi);
Console.read ();
}
}
working with Structs (C # Programming Guide)
struct types are suitable for light-weight objects such as point,Rectangle , and Color . Although you can represent a point as a class , in some cases it is more efficient to use the structure . For example, if you declare an array of 1000 point objects, you need to allocate more memory in order to reference each object, in which case you can conserve resources by using structs. Because the. NET Framework contains an object named Point, we call the struct "CoOrds" instead.
public struct CoOrds
{
public int x, y;
Public CoOrds (int p1, int p2)
{
x = p1;
y = p2;
}
}
The default (parameterless) constructor for declaring a struct is wrong. Default constructors are always provided to initialize struct members to their default values. Initializing an instance field in a struct is also an error.
If you create a struct object using the new operator, the structure object is created and the appropriate constructor is called. Unlike classes, structs can be instantiated without the use of the new operator. If you do not use new, the field remains unassigned and the object is not available until all fields are initialized.
For structs, inheritance does not exist like a class. A struct cannot inherit from another struct or class and cannot be used as a base for a class. However, structs inherit from the base class Object . Structs can implement interfaces in exactly the same way.
Unlike C + +, classes cannot be declared with the struct keyword. In C #, classes and structs are semantically different. A struct is a value type, and a class is a reference type. For more information, see value Types .
Unless you need to reference type semantics, the system will be more efficient at processing smaller classes as structs.
Example 1
The following example shows struct initialization using the default constructor and parameterized constructors.
public struct CoOrds
{
public int x, y;
Public CoOrds (int p1, int p2)
{
x = p1;
y = p2;
}
}//Declare and initialize struct objects.
Class Testcoords
{
static void Main ()
{
Initialize:
CoOrds coords1 = new CoOrds ();
CoOrds coords2 = new CoOrds (10, 10);
Display results:
System.Console.Write ("CoOrds 1:");
System.Console.WriteLine ("x = {0}, y = {1}", coords1.x, COORDS1.Y);
System.Console.Write ("CoOrds 2:");
System.Console.WriteLine ("x = {0}, y = {1}", coords2.x, COORDS2.Y);
}
} output
CoOrds 1:x = 0, y = 0
CoOrds 2:x = ten, y = 10
Example 2
Here is an example of a feature that is unique to a structure. It creates a CoOrds object without using the new operator. If you change the struct to class, the program will not compile.
public struct CoOrds
{
public int x, y;
Public CoOrds (int p1, int p2)
{
x = p1;
y = p2;
}
}
Declare a struct object without "new."
Class Testcoordsnonew
{
static void Main ()
{
Declare an object:
CoOrds coords1;
Initialize:
coords1.x = 10;
Coords1.y = 20;
Display results:
System.Console.Write ("CoOrds 1:");
System.Console.WriteLine ("x = {0}, y = {1}", coords1.x, COORDS1.Y);
}
} output
CoOrds 1:x = ten, y = 20
From: Http://blog.163.com/[email protected]/blog/static/17044246920101032420652/
Structs and their use structs (C #)