Object-oriented C # - Structure

  A structure (StructEntryTable) is one of the five basic types of CTS, a value type, and the behavior of encapsulating data belong to a logical unit, and behavior data represented by a structure member; large class sharing structure most of the same syntax, but the structure is more restricted than the class subject, lightweight structure suitable for the type of representation; define the structure using the struct keyword:

// define a common structure MyStruct 
public  class MyStruct 
{ 
    public  int MyField; // declare a type int public instance field 
    public  void the MyFunc () // declare a public example 
    {
         // do ... 
    } 
}

  1. All structures directly inherited from System.ValueType implicitly, can not be inherited from the specified class or any other structures, i.e., structures do not support inheritance, but may implement one or more interfaces, while the seal structure is also implicit and can not be inherit;

  ※ packing operation will cause the object structure type cast to any type of object or interface type to achieve it, in which case the object will be packaged into a structure type of reference type of managed objects on the heap memory;

  2. Since the structure does not support inheritance, the structure can not be defined as abstract or Sealed;

  3. The structure can not be defined as static, but you can declare static member;

  4. Since the structure does not support inheritance, the structure members can only be declared as public, internal, or private, can not be declared abstract, virtual and sealed;

  ※ For instance member, can not directly initialize the structure declaration;

  ※ For static members can be initialized when they are declared, it can also be initialized in the static constructor;

  ※ structure not declare default constructor (the constructor with no arguments), default constructor structure retained by a compiler, and is always available, its role is to apply the memory space specified size, and initializes all bytes 0 (i.e., default (T));

  ※ structures can declare a custom constructor with parameters, the method body defined constructor must initialize all instances of members, otherwise the compiler will complain, private instance members can only be initialized in the constructor;

  ※ destructor phase structure does not exist, can not be declared destructor;

public  struct MyStruct 
{ 
    public  int MyNum;
     public  String MyStr; 

    public  static  int MyStaticNum = . 1 ; 

    public MyStruct ( int myNum, String myStr) 
    { 
        MyNum = myNum; 
        MyStr = myStr; 
    } 

    static MyStruct () 
    { 
        // static member, can when initialization statement directly, may be static constructor initializes
         // MyStaticNum =. 1; 
    } 
}

 

  Second, by calling the default constructor may be custom constructor initializes the object using the member or separately after the initialization configuration of a statement build a structural example;

  1. Using the new operator or operators to build default configuration example, and call the corresponding constructors:

= MyStruct MyStruct new new MyStruct (); // call the default constructor structure, in which case MyNum is 0, MyStr is null 
myStruct = default (MyStruct); // with the use of new MyStruct () is exactly equivalent 
myStruct = new new MyStruct ( . 1 , " 1 " ); // custom constructor calls structure

  2. Unlike classes, examples of the structure of the new operator may not be used, in any case does not call the constructor does not initialize any instance member, memory allocation efficiency, before accessing an instance field in the field initialized you can:

MyStruct MyStruct; // build structure instance, but do not call the constructor 
myStruct.MyNum = 1 ;
 int myNum = myStruct.MyNum; // need to initialize it before you can access an instance member

  ※ usually applied to only part of the structure and operation of examples of fields storing case;

  ※ only after all instance fields are initialized, and examples thereof can call a method or use as arguments, return values;

  ※ When there is private instance field structure in this way can also be used to build the structure of an example, but also means you can not complete all of its initialization instance fields;

 

  Third, the structure is a value type, and the variable data together, the structure of the type of variable assignment, transmission parameters, etc. The method of operation will return a new variables, and copies all the data (i.e. shallow copy) of the original variables to a new variable, any changes to the new variables do not change the original variable data, new variables can only be re-assigned to the original variables, need extra attention during the collection process value types (such as List <MyStruct>) this point:

// when the data type of an element to modify the set values required, acquire a required receiving variable, and then to modify the set assignment completion 
MyStruct myStruct myStructList = [ 0 ]; 
myStruct.MyNum = 20 is ; 
myStructList [ 0 ] = myStruct ;

  1. The type of structure can be used as air type, value type is still at this time, air can be assigned to a variable of type null;

MyStruct? myStruct = null;

 

  Fourth, since the best practices defined structure:

public  struct MyStruct: the IEquatable <MyStruct> // achieve IEquatable <T> generic interface to 
{
     public  int MyNum; 

    public  the override  BOOL Equals ( Object obj) // will binning argument 
    {
         IF ((obj! IS MyStruct )) 
        { 
            return  to false ; 
        } 
        MyStruct OTHER = (MyStruct) obj; // unpacking 
        return  the this .Equals (OTHER); 
    } 
    public  the override  int GetHashCode () //Avoid packing and provides efficient collection classes implemented using hash 
    {
         return MyNum.GetHashCode (); 
    } 
    public  the override  String the ToString () // avoid packing 
    {
         return MyNum.ToString (); 
    } 

    public  BOOL Equals (MyStruct OTHER) // avoid packing when compared argument, to avoid the use of a generic packing 
    {
         return  the this .MyNum == other.MyNum; 
    } 
    public  static  BOOL  operator == (MyStruct left, right MyStruct) // when compared to commonly used operator == 
    {
         return left.Equals (right); 
    }
    public static bool operator !=(MyStruct left, MyStruct right)
    {
        return !(left == right);
    }
}

※ If desired size comparison, the interface should also implement IComparable <T> carrying both operators <= and> =;

 


If you feel that you have read this article to help, please click the "recommend" button, your recognition is the greatest power of my writing!

Author: Minotauros
Source: https://www.cnblogs.com/minotauros/

This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/minotauros/p/12235949.html