Section 7.1. Defining Structs


7.1. Defining Structs

The syntax for declaring a struct is almost identical to that for a class:

[attributes] [access-modifiers] struct identifier [:interface-list] { struct-members }

Example 7-1 illustrates the definition of a struct. Location represents a point on a two-dimensional surface. Notice that the struct Location is declared exactly as a class would be, except for the use of the keyword struct. Also notice that the Location constructor takes two integers and assigns their value to the instance members, xVal and yVal. The x and y coordinates of Location are declared as properties.

Example 7-1. Creating a struct
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace CreatingAStruct {    public struct Location    {       private int xVal;       private int yVal;       public Location( int xCoordinate, int yCoordinate )       {          xVal = xCoordinate;          yVal = yCoordinate;       }       public int x       {          get          {             return xVal;          }          set          {             xVal = value;          }       }       public int y       {          get          {             return yVal;          }          set          {             yVal = value;          }       }       public override string ToString( )       {          return ( String.Format( "{0}, {1}", xVal, yVal ) );       }    }    public class Tester    {       public void myFunc( Location loc )       {          loc.x = 50;          loc.y = 100;          Console.WriteLine( "In MyFunc loc: {0}", loc );       }       static void Main( )       {          Location loc1 = new Location( 200, 300 );          Console.WriteLine( "Loc1 location: {0}", loc1 );          Tester t = new Tester( );          t.myFunc( loc1 );          Console.WriteLine( "Loc1 location: {0}", loc1 );       }    } } Output: Loc1 location: 200, 300 In MyFunc loc: 50, 100 Loc1 location: 200, 300

Unlike classes, structs don't support inheritance. They implicitly derive from object (as do all types in C#, including the built-in types) but can't inherit from any other class or struct. Structs are also implicitly sealed (that is, no class or struct can derive from a struct). Like classes, however, structs can implement multiple interfaces. Additional differences include the following.


No destructor or custom default constructor

Structs can't have destructors, nor can they have a custom parameterless (default) constructor. If you don't have a constructor, the CLR will initialize your structure and zero out all the fields. If you do provide a nondefault constructor, the CLR initialization will not occur, and so you must initialize all the fields explicitly.


No initialization

You can't initialize an instance field in a struct. Thus, it is illegal to write:

private int xVal = 50; private int yVal = 100;

though that would have been fine had this been a class.

Structs are designed to be simple and lightweight. While private member data promotes data-hiding and encapsulation, some programmers feel it is overkill for structs. They make the member data public, thus simplifying the implementation of the struct. Other programmers feel that properties provide a clean and simple interface, and that good programming practice demands data-hiding even with simple lightweight objects. With the new refactoring ability in Visual Studio, it's easy to turn your previously public variables into private variables with associated public properties. Just right-click on the variable, and choose Refactor Encapsulate Field. Visual Studio will change your public variable to private and create a property with get and set accessors.



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net