当前位置:网站首页>C basic supplement

C basic supplement

2022-07-04 13:41:00 Hua Weiyun

C# Basic supplement

A. Allocation of value types .
There is an area called stack in the virtual memory , We don't know where it is in the address space , There is no need to know in the general development process , What we know is that the value type is assigned here . When the value type is allocated on the stack , It is filled from top to bottom , That is, starting from the high memory address .
For example, the current stack pointer is 100000, This shows that its next free storage space is from 99999 Start , When we're in C# Declare a int Variable of type A, because int The type is four bytes , So it will be allocated in 99996 To 99999 In this storage unit . If we go on to state double Variable B(8 byte ), This variable will be assigned to 99988 To 99995 This storage unit . If the code runs outside their scope , Now A and B Both variables will be deleted , At this time, the order is just the opposite , Delete variables first B, At the same time, the stack pointer will increment 8, That is to point back to 99996 This position ; Next, delete the variable A, The stack pointer points back to 10000. If two variables are declared at the same time . Such as int A,B, At this time, we don't know A and B The order of distribution of , But the compiler will ensure that their deletion order is exactly the opposite of the allocation order .

B. Allocation of reference types .
After understanding the allocation method on the stack , Obviously , Its performance is quite high , At the same time, we also found a disadvantage of it : The lifetime of variables must be nested . This is unacceptable in some cases , Sometimes we need to store some data and ensure that this part of data can be used after the method exits . So , Virtual memory allocates another part of the area , We call it managed heap . A big difference between managed heap and traditional heap is , The managed heap works under the control of the garbage collector . The reference type is allocated on the managed heap , Let's take a look at the allocation process of reference types .
Suppose we need to declare a Person Class and instantiate it .
Person p = new Person();
First , The system will give p This variable allocates storage space on the stack , Of course, it's just a reference , For storage Person The location of the instance on the managed heap , There is no real Person example . Because it just stores an address ( An integer value ), So it will occupy on the stack 4 Bytes of space . Next Person The instance will be stored on the managed heap . Different from the stack , Managed heap is allocated from bottom to top , Suppose this instance needs to occupy 10 Bytes , Suppose the address on the managed heap is 200000, Then it will be allocated in 200000 To 200009 This storage unit .
It should be noted that , This allocation is related to the size of the instance , If the instance is smaller than 85000 byte , It will be allocated to the managed heap . If you exceed 85000 byte , It will be assigned to LOH On .

C# Language does not support multiple inheritance .

1.4.3 Structure type
Structure types are the same as classes , Constructor can be declared 、 Data member 、 Method 、 Properties, etc . The fundamental difference between structure and class is that structure is a value type , Class is a reference type . Unlike classes , A structure cannot derive from another structure or class , Itself cannot be inherited , Therefore, abstract structures cannot be defined , Structure members cannot be controlled by access control words protected modification , You can't use virtual and abstract Modifying structural methods . Destructors cannot be defined in structures . Although structures cannot derive from classes and structures , But structures can inherit interfaces , The methods of structure inheritance interface and class inheritance interface are basically the same . The following example defines a point structure point:

using System;struct point// Structure definition { 	    public int x,y;// Constructors and methods can also be declared in structures , Variables cannot be assigned initial values }class Test{     static void Main()	{         point P1;		P1.x=166;		P1.y=111;		point P2;		P2=P1;// Value passed , send P2.x=166,P2.y=111		point P3=new point();// use new Generate structural variables P3,P3 It is still a value type variable 	}// use new Generate structural variables P3 It only means calling the default constructor , send x=y==0.}
Modifier :

One Access modifier Defines the scope and visibility of a class member .C# The supported access modifiers are as follows :

  • public: All objects can access ;
  • private: The object itself can be accessed inside the object ;
  • protected: Only objects of this class and its subclasses can access
  • internal: Objects of the same assembly can access ;
  • protected internal: Access is limited to the current assembly or type derived from the containing class .

using Usage of :

1. using Instructions : Introduce namespace

This is the most common usage , for example :

using System;using Namespace1.SubNameSpace;

2. using static Instructions : Specifies the type whose static members can be accessed without specifying a type name

using static System.Math;var = PI; //  Use it directly System.Math.PI

3. names

using Project = PC.MyCompany.Project;

4. using sentence : Bind instance to code

using (Font font3 = new Font("Arial", 10.0f),            font4 = new Font("Arial", 10.0f)){    // Use font3 and font4.}

At the end of the snippet , Automatically call font3 and font4 Of Dispose Method , Release instance .

About the supplement of constructor :

For parent and child classes , When subclasses call constructors, they will first call the parent constructor and then the subclass constructor . alike , If it is multiple inheritance , Then when the later subclasses call the constructor, they will call it from the constructor of the uppermost parent class once to the next , Until your constructor .

enumeration
C# Enumeration learning needs attention :

System.Enum Type is the abstract base class of all enumeration types ( It is a unique type different from the base type of enumeration type ), And from System.Enum Inherited members are available in any enumeration type . There are from any enumeration type to System.Enum Packing conversion for , And there are System.Enum Unboxing conversion to any enumeration type .System.Enum Itself is not an enumeration type . contrary , It is a class type , All enumeration types are derived from it . type System.Enum From the type System.ValueType The derived , The latter is from type object The derived . At run time , type System.Enum The value of can be null Or a reference to a boxed value of any enumeration type .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041155517485.html