In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Use attributes instead of accessible data members. Second, use run-time constants (readonly) instead of compile-time constants (const)
1. C # has two types of constants: compile-time constants and run-time constants.
two。 Try to use run-time constants instead of compile-time constants.
/ / compile-time constant / public const int Num = 100; / run-time constant / public static readonly int Year = 2017
3. A compile-time constant can only be used for numbers and strings, and a runtime constant is also a constant because it cannot be modified again after the constructor is executed.
4.const is more efficient but less flexible than readonly.
Third, it is recommended to use is or as operators instead of casting
1.as is more efficient and safe than forced transfer.
The 2.as operator cannot be used with a value type because the value type can never be null.
Fourth, use the Conditional feature instead of # if conditional compilation. Fifth, provide the ToString () method for the type
1. An appropriate version of ToString () should be provided for the type, otherwise the consumer will construct the row based on some properties of the class and use it for display.
The ToString () method provided by 2.object by default returns the full name of the type, which doesn't make much sense. Such as: System.Drawing.Rect.
3. Override all types of ToString () to simply and clearly display the summary information of the object.
6. Understand the relationship between several equal judgments
1. The system provides four functions to judge whether two objects are "equal".
two。 For the first two methods, we should never redefine them. We usually rewrite the Equals method.
3. The type that overrides Equals also implements IEquatable, and if it is a structure, it needs to implement IStructuralEquatable.
4. Referencing the same DataRow will be considered equal, and if you want to compare the content instead of the reference address, you should override the Equals () instance method.
The principle of rewriting the 5.Equals () instance method: for all value types, the Equals () method should be overridden, and for reference types, the method should only be overridden if it does not meet the need. Overriding the method also requires overriding the GetHashCode () method.
6.operator = = (): whenever you create a value type, you must redefine operator = = (), because the system defaults to comparing whether the two values are equal through reflection, which is too inefficient.
7. Understand the trap of GetHashCode ()
1. For most of the types we implement, avoid implementing GetHashCode ().
Overloaded versions of 2.GetHashCode () must follow the following three principles:
(1) if two objects are equal (defined by operator = =), then they must generate the same hash code.
(2) for any object, Ameme A. GetHashCode () must remain the same.
(3) for all inputs, the hash function should generate hash codes according to random distribution among all integers.
It is recommended to use query syntax instead of loops
Example:
/ / 1. Use the loop var foo = new int [100]; for (int I = 0; I < 100; iSum +) {foo [I] = I * I;} / / use the query syntax var foo2 = (from n in Enumerable.Range (0,100) select n * n). ToArray ()
1. Some method syntax does not have corresponding query syntax, such as Take, TaskWhile, Skip, SkipWhile, Min, Max, etc., so you need to use method syntax.
Avoid using the conversion operator 10 in API and use optional parameters to reduce the number of method overloads
1. For the first release of an assembly, you can optionally use optional and named parameters. In subsequent releases, you must create overloads for additional parameters. Only in this way can we ensure that the current program can still run normally. In addition, in any subsequent release, avoid changing the name of the parameter because the parameter name has become part of the public interface.
11. Understand the advantages of short methods
1. We'd better write the clearest code possible and leave the optimization to JIT. A common misoptimization is that we put a lot of logic in a function, thinking that this will reduce the extra method call overhead.
Public string Test (bool isTrue) {var sb = new StringBuilder (); if (isTrue) {sb.AppendLine ("A"); sb.AppendLine ("B"); sb.AppendLine ("C");} else {sb.AppendLine ("E") Sb.AppendLine ("F"); sb.AppendLine ("G");} return sb.ToString ();}
When the Test method is called for the first time, both branches of if-else are compiled by JIT, but only one of them needs to be compiled, modified:
Public string Test2 (bool isTrue) {var sb = new StringBuilder (); if (isTrue) {return Method1 ();} else {return Method2 ();}}
Now that you have split the methods, the two methods can be JIT compiled as needed without having to compile them all for the first time.
two。 You can selectively extract more than dozens of statements in the if-else branch, or a branch dedicated to dealing with errors in the program, or the code in each case in the switch statement.
3. The short approach, which generally contains fewer local variables, makes it easier for JIT to select registers, that is, which local variables are placed in registers rather than on the stack.
4. Try to write short methods.
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.