Copy a struct : struct « Class Interface « C# / C Sharp






Copy a struct

Copy a struct
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Copy a struct. 
 
using System; 
 
// Define a structure. 
struct MyStruct { 
  public int x; 
} 
 
// Demonstrate structure assignment. 
public class StructAssignment { 
  public static void Main() { 
    MyStruct a; 
    MyStruct b; 
 
    a.x = 10; 
    b.x = 20; 
 
    Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); 
 
    a = b; 
    b.x = 30; 
 
    Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); 
  } 
}


           
       








Related examples in the same category

1.Structs And Enums
2.Define struct and use it
3.Demonstrate a structureDemonstrate a structure
4.Structures are good when grouping dataStructures are good when grouping data
5.demonstrates a custom constructor function for a structuredemonstrates a custom constructor function for a structure
6.Defining functions for structs
7.demonstrates using a structure to return a group of variables from a functiondemonstrates using a structure to return a group of variables from a function
8.Demonstates assignment operator on structures and classes.Demonstates assignment operator on structures and classes.
9.Issue an error message if you do not initialize all of the fields in a structure
10.Illustrates the use of a structIllustrates the use of a struct
11.C# always creates a structure instance as a value-type variable even using the new operatorC# always creates a structure instance as a value-type variable even using the new operator
12.Calling a Function with a Structure ParameterCalling a Function with a Structure Parameter
13.Structs (Value Types):A Point StructStructs (Value Types):A Point Struct
14.Structs (Value Types):Structs and ConstructorsStructs (Value Types):Structs and Constructors
15.Conversions Between Structs 1
16.Conversions Between Structs 2