In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "introduction of fixed and active variables in C#". In daily operation, I believe that many people have doubts about the introduction of fixed and active variables in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "introduction of fixed and active variables in C#"! Next, please follow the editor to study!
& and fixed classify variables into two categories: C # fixed and active variables.
The existence of the fixed variable * * is not affected by the operation of GC. Active variables (such as local variables, value parameters, and indirect pointers) are relocated or released by GC (for example, fields in objects, underlying data arrays).
& there is no limit to the address of a fixed variable. However, the active variable is easily affected by GC, its address can only be obtained through fixed, and the address will only be valid for the entire cycle of the fixed statement.
Static fields are also active variables. Others marked ref or out are also active variables, even if this parameter is passed to a fixed parameter. * variables referenced by indirect pointers are always fixed.
Pointer conversion
In an insecure context, the following transformations are implicit:
◆ from any pointer type to void*
◆ from null to any pointer type
In addition, the following transformations are explicit:
◆ from any pointer type to another pointer type
◆ from sbyte, byte, short, ushort, int, uint, long, or ulong to other types
◆ from any type to sbyte, byte, short, ushort, int, uint, long, or ulong
Pointer conversion does not change the value of the pointer. In other words, the value of the address is not affected before and after the pointer conversion.
When the conversion occurs, when the result pointer is not arranged according to the correct pointer type, the result is unpredictable when the result is accessed.
Here is an example:
Char c = 'Aids; char* pc = & c; void* pv = pc; int* pi = (int*) pv; int i = * pi; / / undefined * pi = 123456; / / undefined
The following example prints the 8-byte value of double:
UsingSystem; classTest {unsafestaticvoidMain () {doubled=123.456e23; unsafe {byte*pb= (byte*) & d; for (inti=0;iy=20; Console.WriteLine (p-> ToString ());}
Or
ClassTest {staticvoidMain () {Pointpoint; unsafe {Point*p=&point; (* p) .x = 10; (* p) .y = 20; Console.WriteLine ((* p) .ToString ());}
Pointer element access
In pointer element access P [E], P must be of a type other than void*, and E must be implicitly converted to expressions of int, uint, long, or ulong.
The effect of P [E] is the same as * (P + E). For example:
ClassTest {staticvoidMain () {unsafe {char*p=stackallocchar [256]; for (inti=0;i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.