Wiki

Changes between Initial Version and Version 1 of Struct

Show
Ignore:
Timestamp:
05/14/10 13:22:52 (8 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Struct

    v1 v1  
     1= Struct Definition =  
     2 
     3Structs are similar to classes but more limited in ways that make them suitable [[BR]] 
     4for representing lightweight value-container-only objects ( like a Point or Rectangle). 
     5 
     6Unlike classes (reference type) they are a value type and they cannot be inherited or inherit[[BR]] 
     7from another class or struct.  
     8 
     9Other differences from classes:[[BR]] 
     10    * Within a struct declaration, fields cannot be initialized unless they are declared as const or static. 
     11    * A struct may not declare a default constructor (a constructor without parameters) or a destructor. 
     12    * Structs can only declare constructors that have parameters. 
     13    * Structs are copied on assignment and when passed into methods. [[BR]] 
     14      When a struct is assigned to a new variable or passed to a method, all the data is copied, and any modification to the [[BR]] 
     15      new copy does not change the data for the original copy.  
     16 
     17However 
     18    * A struct can implement interfaces. 
     19    * A struct can be used as a nullable type and can be assigned a null value. 
     20 
     21Structures and their contained instance members are public by default though this may be[[BR]] 
     22overridden using the normal [wiki:AccessModifiers access modifiers] syntax. 
     23 
     24Idomatic usage suggests structs should not modify their accessmodifiers for visibility and[[BR]] 
     25should not declare any methods (If this is deemed necessary you should use a class instead).[[BR]] 
     26They should only be used as publicly accessible value containers.  
     27 
     28The syntax/grammar for declaring structures is similar to that of classes [[BR]] 
     29(barring the '''struct''' keyword and lack of the '''inherits''' clause) 
     30 
     31== Grammar == 
     32{{{ 
     33struct <StructureName>   
     34    [is <AccessModifiers>] 
     35    [has <Attributes>] 
     36    [where <GenericParam> must be <GenericConstraints>] 
     37    [implements <InterfaceName> [, <InterfaceName>]...] 
     38    [<DocString>] 
     39    [ 
     40        [<Variables>] 
     41        [<Constants>] 
     42        [<Properties>] 
     43        [<Methods> 
     44        [<Enums>] 
     45        [<Sigs>] 
     46        [<Invariants>] 
     47        [<Tests>] 
     48        [<SharedClause>] 
     49    ]... 
     50 
     51# empty (placeholder) structure 
     52struct <StructureName>   
     53    [is <AccessModifiers>] 
     54    [has <Attributes>] 
     55    [where <GenericParam> must be <GenericConstraints>] 
     56    [implements <InterfaceName> [, <InterfaceName>]...] 
     57    [<DocString>] 
     58        pass 
     59}}} 
     60 
     61== Platform == 
     62 
     63in .Net,  all structs inherit directly from System.!ValueType, which inherits from System.Object. 
     64 
     65== Examples == 
     66{{{ 
     67# a simple Point-like struct 
     68struct CoOrds 
     69    var x as int 
     70    var y as int 
     71 
     72    cue init(p1 as int, p2 as int) 
     73        .x = p1 
     74        .y = p2 
     75 
     76 
     77#// Declare and initialize struct objects. 
     78class TestCoOrds 
     79    def main is shared 
     80        # Initialize:    
     81        coords1 = CoOrds() 
     82        coords2 = CoOrds(10, 10) 
     83 
     84        # Display results: 
     85        print 'CoOrds 1: ' stop 
     86        print 'x = [coords1.x], y = [coords1.y]' 
     87 
     88        print 'CoOrds 2: ' stop 
     89        print 'x = [coords2.x], y = [coords2.y]' 
     90         
     91        assert coords1.x == 0 and coords1.y == 0 
     92        assert coords2.x == 10 and coords2.y == 10 
     93 
     94/# Output: 
     95    CoOrds 1: x = 0, y = 0 
     96    CoOrds 2: x = 10, y = 10 
     97#/ 
     98 
     99 
     100}}} 
     101 
     102== Notes == 
     103 
     104== See Also == 
     105[http://msdn.microsoft.com/en-us/library/ah19swz4.aspx struct(C# Reference)] 
     106 
     107[http://msdn.microsoft.com/en-us/library/saxz13w4.aspx Structs(C# Programming Guide)] 
     108 
     109[http://msdn.microsoft.com/en-us/library/0taef578.aspx Using Structs( C# Programming Guide)] 
     110 
     111 
     112[wiki:LanguageTopics Back to LanguageTopics]