130 likes | 225 Views
struct ValueType. CNS 3260 Version 1.0 Dennis A. Fairclough. Overview. struct Value Type Userdefined Type struct <identifier> { //… body } //of type Data Members Method Members Do not directly use heap allocation Sealed (Cannot be inherited from)
E N D
structValueType CNS 3260 Version 1.0 Dennis A. Fairclough
Overview • struct • Value Type • Userdefined Type • struct <identifier> { //… body } //of type • Data Members • Method Members • Do not directly use heap allocation • Sealed (Cannot be inherited from) • Inherits from ValueType which inherits from System.Object • Can implement (inherit) interface(s) • Must initialize variables before their use • Interface reference variable can reference a struct
struct • Lightweight Objects • structtypes do not support user-specified inheritance, and all struct types implicitly inherit from ValueTypeobject. • struct may implement multiple interfaces. • struct’s maybe generic class types. • struct type directly stores the data of the struct, whereas a variable of a class type stores a reference to a dynamically allocated object.
Some struct Rules • Are ValueTypes • Default Member Access – private • Instance default Constructor – Only compiler writes • Instance default Constructor – Always present • Cannot have instance field initializers • Instance constructors are invoked by new operator • simply returns the struct value itself (typically in a temporary location on the stack), and this value is then copied as necessary. • User defined static default Constructor • Sealed • Can implement (inherit) interfaces • May contain one or Main() methods
Some struct Rules • Creates a new declaration space • Inherits directly from System.ValueType • May have • Instance Member Data • Instance Member Methods • static Member Data • static Member Methods • May not have a user defined default Constructor only compiler defined default Constructor • static Constructors – Default & parameterized • Access Modifiers • public, private (default), internal • Uninitialized Member Data – NOT ALLOWED!
Some struct Rules • Allows overloaded operators (really methods) • Allows overloaded methods • Allows properties and indexers • Allows nested types • Members accessed with “.” operator • Simple Types – Really structs! • i.e. int, uint, double, char, etc. • Cannot be declared “const” – simple types can! • Has a “this” variable • Cannot be static • Can or must be boxed & unboxed
struct public structMData : IMyTester { public int _idata; //member variables public double _ddata; //public MData() //Not allowed in struct's (Compiler always writes) //{ //} public MData(intidata, double ddata) //member methods { _idata= idata; _ddata= ddata; } } public void Main() { MData data0; //member variables NOT initialized intiresult = _idata; //error uninitialized variable MData data1 = new MData(); //member variables initialized (default) MData data2 = new MData(10,5.5); //member variables intialized (params) }
struct • Memory Allocation • Stack for Method Variables • Within object for instance object • box to make reference variable • unbox to convert back to ValueType variable
Boxing & Unboxing • MDatamystruct = new MData(); • object myobj = mystruct; • MDataotherstruct = (MData)myobj;
Interface Variables • IMyTester Iref = new Mdata(10,5.5);
struct • sealed • Inherits -> ValueType -> object • struct MData : IMyTester{ } • Member Variables must be initialized before use.