Structs

Structs are similar to classes, except that classes are reference types and structs are value types. In other words, if an instance of a struct is passed to a method, a copy is made and passed. If an instance of a class is passed to a method, a reference to that object is passed. So, structs are useful for creating user-defined types that behave like built-in types. Listing A.5 shows a simple struct example.

Listing A.5. A Simple Point Struct Declaration
 1: using System; 2: 3: struct Point{ 4: public double X; 5: public double Y; 6: 7: public void print(){ 8: Console.WriteLine("(" + X + ", " + Y + ")" ); 9: } 10: 11: } // End of Point struct 12: 13: public class PointStructTester{ 14: public static void Main(){ 15: Point p; 16: p.X = ...

Get Building e-Commerce Sites with the .NET Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.