C # study notes 6,
1.Structure: In addition to attributes and fields, the structure can also include methods and constructors, but cannot contain the constructor for token recognition (No parameter. Sometimes (for example, when instantiating an array), the Value Type constructor is not called, because all Array Memory is converted to zero for initialization, to avoid inconsistency caused by occasional calls of the default constructor, C # explicitly defines the default constructor, because the compiler will assign the instance field value during declaration to the Type constructor. All fields in struct must be initialized in the constructor. If this is not done, a compilation error occurs. You can view the code of the Angle structure.
2.Structure Inheritance and interface: All value types are sealed. In addition, all value types are derived from system. valueType, which means that the inheritance chain of struct is from object to ValueType to struct. Value types can also implement interfaces. Many interfaces are fixed components of the implementation interface framework, such as IComparilble and IFormattable.
3.Packing and unpacking: The value type is changed to the reference type, as follows:
(1) first, allocate memory in the heap, which will be used to store value-type data and a little additional overhead;
(2) then a memory replication action occurs, and the value type data on the stack is copied to the allocated position on the stack;
(3) Finally, the object or interface reference is updated, pointing to the location on the stack;
The unbox action is to change the reference type to the value type, as shown in the following figure: According to the definition, the pencil command unbox only unreferences the data on the stack, and does not include the action of copying data from the stack to the stack. However, in C # language, a copy operation occurs after the box is split when there are too many data records. Packing and unpacking are important because packing has some impact on performance and behavior. Developers can view the number of box/unbox commands in a specific code snippet. The code in BoxAndUnbox () has been packed and unpacked multiple times, so it is unreasonable to write the code.
4.Enumeration: enumeration is slightly different from other value types, because the inheritance chain of enumeration is from System. ValueType to System. Enum, and then to enum.
5.Enumeration and String Conversion: After ToString () is enumerated, the enumerated identifier is output, using Enum. parse or Enum. the TryParse method can convert a string to enumeration. The next method is. the generic method added in Net4.0. You can also use the Enum. IsDefined () method to check whether a value is included in an enumeration.
6.Enumeration is used as a "Bit Flag:
(1) You can view the following "FileAttributes" enumeration settings (that is, System. IO. FileAttributes settings). After being a bit flag, its values can be freely combined. Therefore, you can use the Or operator to join the enumerated values. In this example, the BitFlag () method is used. Of course, each value in the enumeration does not necessarily correspond to only one flag. You can define additional enumeration values for common logo combinations.
(2) When the Bit Flag type is used, the bit flag enumeration should contain the [FlagsAttribute] feature. This flag indicates that multiple enumeration values can be used in combination. In addition, it changes ToString () and Parse () method. For example, if you call the ToString () method for an enumeration that has been modified by FlagsAttribute, the corresponding string (for example, BitFlag2 () is output for each configured enumeration mark ), without this modifier, the returned value is the combined value.
public struct Angle{ public Angle(int hours, int minutes, int seconds) { Hours = hours; Minutes = minutes; Seconds = seconds; } public int Hours { get; set; } public int Minutes { get; set; } public int Seconds { get; set; } public Angle Move(int hours, int minutes, int seconds) { return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds); }}[Flags]public enum FileAttributes{ ReadOnly = 1 << 0, Hidden = 1 << 1, System = 1 << 2, Directory = 1 << 3, Archive = 1 << 5, Device = 1 << 6, Normal = 1 << 7, Temporary = 1 << 8, SparseFile = 1 << 9, ReparsePoint = 1 << 10, Compressed = 1 << 11, Offline = 1 << 12, NotContentIndexed = 1 << 13, Encrypted = 1 << 14}public void BitFlag(){ string fileName = @"enumtest.txt"; FileInfo file = new FileInfo(fileName); file.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; Console.WriteLine("{0} | {1} = {2}", FileAttributes.Hidden, FileAttributes.ReadOnly, (int)file.Attributes); if ((file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) { throw new Exception("File is not hidden"); } if ((file.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly) { throw new Exception("File is not read-only"); } //....}public void BitFlag2(){ string fileName = @"enumtest.txt"; FileInfo file = new FileInfo(fileName); file.Open(FileMode.Create).Close(); FileAttributes startingAttributes = file.Attributes; file.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; Console.WriteLine("\"{0}\" output as \"{1}\"", file.Attributes.ToString().Replace(",", "|"), file.Attributes); FileAttributes attributes; Enum.TryParse(file.Attributes.ToString(), out attributes); Console.WriteLine(attributes); File.SetAttributes(fileName, startingAttributes); file.Delete();}public void BoxAndUnbox(){ int totalCount; ArrayList list = new ArrayList(); Console.Write("Enter a number between 2 to 1000:"); totalCount = int.Parse(Console.ReadLine()); list.Add((double)0); list.Add((double)1); for (int i = 2; i < totalCount; i++) { list.Add((double)list[i - 1] + (double)list[i - 2]); } foreach (double num in list) { Console.Write("{0},", num); }}
---------------------- The above content is organized according to "C # the third edition of this topic ".