Csharp/CSharp Tutorial/struct/struct
Содержание
A simple struct with method
<source lang="csharp">using System; struct Fraction {
public int numerator; public int denominator; public void Print( ) { Console.WriteLine( "{0}/{1}", numerator, denominator ); }
} public class MainClass {
public static void Main( ) { Fraction f; f.numerator = 5; f.denominator = 10; f.Print( ); Fraction f2 = f; f.Print( ); f2.numerator = 1; f.Print( ); f2.Print( ); }
}</source>
5/10 5/10 5/10 1/10
class vs struct
<source lang="csharp">using System; using System.Collections.Generic; using System.Text;
class MyClass {
public int val;
} struct myStruct {
public int val;
} class Program {
static void Main(string[] args) { MyClass objectA = new MyClass(); MyClass objectB = objectA; objectA.val = 10; objectB.val = 20; myStruct structA = new myStruct(); myStruct structB = structA; structA.val = 30; structB.val = 40; Console.WriteLine("objectA.val = {0}", objectA.val); Console.WriteLine("objectB.val = {0}", objectB.val); Console.WriteLine("structA.val = {0}", structA.val); Console.WriteLine("structB.val = {0}", structB.val); }
}</source>
Declare a simple struct
<source lang="csharp">struct Fraction {
public int numerator; public int denominator;
} public class MainClass {
public static void Main( ) { Fraction f; f.numerator = 5; f.denominator = 10; }
}</source>
Difference between class and struct during the reference passing
<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;
class MyClass { public int val; } struct myStruct { public int val; } class Program { static void Main(string[] args) { MyClass objectA = new MyClass(); MyClass objectB = objectA; objectA.val = 10; objectB.val = 20; myStruct structA = new myStruct(); myStruct structB = structA; structA.val = 30; structB.val = 40; Console.WriteLine("objectA.val = {0}", objectA.val); Console.WriteLine("objectB.val = {0}", objectB.val); Console.WriteLine("structA.val = {0}", structA.val); Console.WriteLine("structB.val = {0}", structB.val); } }</source>
Reference value type in a struct
<source lang="csharp">using System; struct Point {
public int X; public int Y;
} class MainClass {
static void Main() { Point p1, p2, p3; p1.X = 10; p1.Y = 10; p2.X = 20; p2.Y = 20; p3.X = p1.X + p2.X; p3.Y = p1.Y + p2.Y; Console.WriteLine("p1: {0}, {1}", p1.X, p1.Y); Console.WriteLine("p2: {0}, {1}", p2.X, p2.Y); Console.WriteLine("p3: {0}, {1}", p3.X, p3.Y); }
}</source>
p1: 10, 10 p2: 20, 20 p3: 30, 30
Structures
A struct is similar to a class, but is a value type, not a reference type.
Here is the general form of a struct:
<source lang="csharp">struct name : interfaces {
// member declarations }</source>
- Structures cannot inherit other structures or classes
- Structures cannot be used as a base for other structures or classes.
- A structure can implement one or more interfaces.
- Structure members can be methods, fields, indexers, properties, operator methods, and events.
- Structures can also define constructors, but not destructors.
- You cannot define a default (parameterless) constructor for a structure.
- A default constructor is automatically defined for all structures, and this default constructor can"t be changed.
- Structure members cannot be specified as abstract, virtual, or protected.
- When new is used, the specified constructor is called.
- When new is not used, the object is still created, but it is not initialized.
- When you assign one structure to another, a copy of the structure object is made.
struct with value types
<source lang="csharp">using System; struct PointerStruct {
public int x, y;
} class MainClass {
public static void Main(string[] args) { PointerStruct f1 = new PointerStruct(); f1.x = 100; f1.y = 100; PointerStruct f2 = f1; Console.WriteLine("F1.x = {0}", f1.x); Console.WriteLine("F1.y = {0}", f1.y); Console.WriteLine("F2.x = {0}", f2.x); Console.WriteLine("F2.y = {0}", f2.y); Console.WriteLine("-> Changing f2.x"); f2.x = 900; Console.WriteLine("F2.x = {0}", f2.x); Console.WriteLine("F1.x = {0}\n", f1.x); }
}</source>
F1.x = 100 F1.y = 100 F2.x = 100 F2.y = 100 -> Changing f2.x F2.x = 900 F1.x = 100
struct with value types and ref types
<source lang="csharp">using System; class MyClass {
public string x; public MyClass(string s) { x = s; }
} struct MyStruct {
public MyClass refType; // Ref type. public int valueType; // Val type public MyStruct(string s) { refType = new MyClass(s); valueType = 9; }
} class MainClass {
public static void Main(string[] args) { MyStruct valWithRef = new MyStruct("Initial value"); valWithRef.valueType = 6; MyStruct valWithRef2; valWithRef2 = valWithRef; valWithRef2.refType.x = "I am NEW!"; valWithRef2.valueType = 7; Console.WriteLine("Values after change:"); Console.WriteLine("valWithRef.refType.x is {0}", valWithRef.refType.x); Console.WriteLine("valWithRef2.refType.x is {0}", valWithRef2.refType.x); Console.WriteLine("valWithRef.valueType is {0}", valWithRef.valueType); Console.WriteLine("valWithRef2.valueType is {0}", valWithRef2.valueType); }
}</source>
Values after change: valWithRef.refType.x is I am NEW! valWithRef2.refType.x is I am NEW! valWithRef.valueType is 6 valWithRef2.valueType is 7