1 / 12

struct ValueType

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)

erelah
Télécharger la présentation

struct ValueType

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. structValueType CNS 3260 Version 1.0 Dennis A. Fairclough

  2. 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

  3. 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.

  4. 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

  5. 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!

  6. 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

  7. 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) }

  8. struct • Memory Allocation • Stack for Method Variables • Within object for instance object • box to make reference variable • unbox to convert back to ValueType variable

  9. Boxing & Unboxing • MDatamystruct = new MData(); • object myobj = mystruct; • MDataotherstruct = (MData)myobj;

  10. Interface Variables • IMyTester Iref = new Mdata(10,5.5);

  11. struct • sealed • Inherits -> ValueType -> object • struct MData : IMyTester{ } • Member Variables must be initialized before use.

  12. What did you learn? • ?

More Related