In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "C# conditional compilation, inline functions, CLS example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "C# conditional compilation, inline functions, CLS example analysis" this article.
1. Conditional compilation
# if conditional compilation hides the non-conditional (# else if) code, which is likely to be ignored in our development. When we switch conditional constants to this part of the code, it is likely to cause an error for various reasons.
If you use features for conditional compilation tags, you can notice this part of the code during development.
[Conditional ("DEBUG")]
For example, when using modify all references-modify a class member variable or static variable name, the code in the # if non-conditional will not be modified because this part of the code is "invalid" and the code using [Conditional ("DEBUG")] is independent of the condition and will be modified synchronously.
Methods such as Conditional feature tags remain valid during development and may be excluded at compile time.
The code snippet can only use # if, and if it is a single method, you can use Conditional.
2. MethodImpl characteristics
This feature specifies the details of how to implement the method in the System.Runtime.CompilerServices namespace.
For more information on how to use inline functions, please refer to https://www.yisu.com/article/242567.htm
The MethodImpl feature can affect the behavior of the JIT compiler.
You cannot use MemberInfo.GetCustomAttributes to get the information about this feature, that is, you can't get MethodImpl-related information (reflection) by getting the property. You can only call MethodInfo.GetMethodImplementationFlags () or ConstructorInfo.GetMethodImplementationFlags () to retrieve it.
MethodImpl can be used on methods as well as constructors.
MethodImplOptions is used to set the compilation behavior. Enumerated values can be combined. The enumeration is described as follows:
The enumeration value indicates that AggressiveInlining256 should inline the method if possible. AggressiveOptimization512 this method includes a hot path and should be optimized. ForwardRef16 has declared this method, but provides the implementation in other locations. The InternalCall4096 call is internal, that is, it calls methods implemented in the common language runtime. NoInlining8 this method cannot be an inline method. Inlining is an optimization in which the method call is replaced with the method body. When NoOptimization64 debugs possible code generation problems, this method is not optimized by the just-in-time (JIT) compiler or native code generation (see Ngen.exe). PreserveSig128 exports the method signature exactly as declared. Synchronized32 this method can only be executed on one thread at a time. Static methods are locked on types, while instance methods are locked on instances. Only one thread can execute in any instance function, and only one thread can execute in a static function of any class. Unmanaged4 this method is implemented in unmanaged code.
The Synchronized-decorated approach avoids some of the problems in multithreading, but it is not recommended to use locking instances or type locks on common types, because Synchronized can lock public types and instances of code that are not its own. This can lead to deadlocks or other synchronization problems.
This means that if the shared member has already set a lock, it should no longer be used in the Synchronized method, so double locking can easily lead to deadlocks and other problems.
3 、 CLSCompliantAttribute
Indicates whether the program element conforms to the common language specification (CLS).
The CLS specification can be referred to:
Https://docs.microsoft.com/en-us/dotnet/standard/language-independence
Global enable method:
Add an AssemblyAttribytes.cs file in the program directory, or open the obj directory, find the file at the end of the AssemblyAttributes.cs, such as. Net CoreApp, Version=v3.1.AssemblyAttributes.cs, and add:
Using System; / / do not add [assembly: CLSCompliant (true)] if you already have this line.
You can then use the [CLSCompliant (true)] feature in your code.
Partial enable:
It can also be used on members such as classes:
[assembly: CLSCompliant (true)]
You can apply features to the following program elements of CLSCompliantAttribute: assemblies, modules, classes, structures, enumerations, constructors, methods, properties, fields, events, interfaces, delegates, parameters, and return values. However, the concept of CLS compliance applies only to assemblies, modules, types, and members of types.
By default, the program does not check whether the code meets the CLS requirements, but if yours can be public (code sharing, Nuget release, etc.), it is recommended to use [assembly: CLSCompliant (true)] to indicate that your library meets the CLS requirements.
High-quality code is particularly important in team development and when sharing code within the team, so it is necessary to use tools to check the code, such as roslyn static analysis, sonar scanning, etc., or you can use the above features to automatically use CLS checking.
Part of the CLS requirements:
Unsigned types should not be part of the public interface of the class (private members can use them), such as UInt32, which are types of C # but are not part of the CLS Standard.
Unsafe types such as pointers cannot be used with public members, but unsafe code should not be used in public methods. (private members can use it).
Class and member names should not be duplicated. Although case-sensitive in C #, CLS does not recommend non-overloaded functions with the same name, such as MYTEST and Mytest.
Only properties and methods can be overloaded, not operators. Overloaded operators can easily lead to program errors when the caller does not know it, and it is difficult to troubleshoot overloaded operators.
We can compile the following code and try using CLSCompliant:
[assembly: CLSCompliant (true)] [CLSCompliant (true)] public class Test {public void MyMethod () {} public void MYMETHOD () {}}
Warning in IDE: warning CS3005: only the identifier "Test.MYMETHOD ()" with different case does not conform to CLS, and Warn is prompted at compile time. Of course, it does not prevent compilation, nor does it affect the running of the program.
In summary, if you want to mark an assembly CLS specification, you can use the [assembly: CLSCompliant (true)] attribute.
The [CLSCompliant (true)] feature indicates that the element conforms to the CLS specification, and the compiler or IDE will check your code to see if it really conforms to the specification.
If you want to write code that doesn't conform to the specification, you can use [CLSCompliant (false)].
4. Customize type aliases if necessary
C # can also define type aliases.
Using intbyte = System.Int32;using intkb = System.Int32;using intmb = System.Int32;using intgb = System.Int32;using inttb = System.Int32; byte [] fileByte = File.ReadAllBytes (". / 666.txt"); intmb size = fileByte.Length / 1024
In some cases, using aliases can improve the readability of your code. Don't use the above code for a real project. I'm just writing an example, which is not an appropriate application scenario.
I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.