Code Analysis notes

Source: Internet
Author: User
Code Analysis warning for managed code (sorted by checkid)

Http://msdn.microsoft.com/zh-cn/library/dd380629

I made a code analysis today. The notes are as follows:

 

1. The following prompt appears for tolower ():

Warning 4 ca1304: Microsoft. Globalization: Because the behavior of 'string. tolower () 'could vary based on the current user's locale settings,
Replace this call in 'commonprovider. ispathequal (this string, string) 'with a call to 'string. tolower (cultureinfo )'.
If the result of 'string. tolower (cultureinfo) 'will be displayed to the user, specify 'cultureinfo. currentculture' as the 'cultureinfo' parameter.
Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'cultureinfo. invariantculture '.

When it comes to the regional language, I will change it to tolowerinvariant (). This is always okay, but there are prompts.

Ca1308: normalize string to uppercase
The string should be normalized to uppercase letters. A small number of characters cannot be converted back after being converted to lowercase letters. Round-trip conversion is to convert a character from one region to another to indicate the region settings for different character data, and then accurately retrieve the original character from the converted character.

So change toupperinvariant.

2. ca1704: The identifier should be correctly spelled

Generally, we do not recommend abbreviations, especially the first letter of each word, or half of the word.
For example,
Public bool blibrary = false;
Public list <performancebase> getprevpage ()

3. ca1002: do not publish the generic list

Warning 37 ca1002: Microsoft. DESIGN: Change 'list <drivetype> 'in' ifiletree. setdrivefilterforinclude (list <drivetype>) 'to use collection <t>, readonlycollection <t> or keyedcollection <K, V> E: \ Nero \ source \ gen_n11 \ Dev \ backup \ pb_backup \ biulab \ filepolicerdemo \ interface \ ifiletree. CS 88 fileexploredemo
Ms indicates that the return value type is preferably collection <t>, readonlycollection <t> or keyedcollection <K, V>.
If the attributes in the class are set types, it is best to use these three types,
In fact, it is quite troublesome to do so, and sometimes it is uncomfortable to use LINQ.
System. Collections. Generic. List <t> is a generic set designed for performance rather than inheritance. System. Collections. Generic. List <t> does not contain virtual members that are easier to change the behavior of an inherited class. The following generic set is designed for the inheritance function and should be made public outside of system. Collections. Generic. List <t>.
System. Collections. objectmodel. collection <t>
System. Collections. objectmodel. readonlycollection <t>
System. Collections. objectmodel. keyedcollection <tkey, titem>

4. ca2227: The set attribute should be read-only.

Writing like below does not conform to the specifications. To ensure security, this set should not be set, and the initialization of the set should be placed in the class constructor. When you need to change the set, call Add/addrange and other methods. This prevents the entire set from being given to new.
/// <Summary>
/// Childnodes
/// </Summary>
Private list <filecyclerdata> _ childnodes;

/// <Summary>
/// Childnodes
/// </Summary>
Public override list <filepolicerdata> childnodes
{
Get
{
Return _ childnodes;
}
Set
{
If (_ childnodes! = Value)
{
_ Childnodes = value;
}
}
}
In fact, this operation also brings some trouble.

5. ca1721: the attribute name should not conflict with the get method.
This seems to be taken into consideration for XAML.
For example, a class has an attribute named icon and a method named getIcon (). This is not acceptable. Get is a keyword and sensitive. Change it to loadicon.

6. ca1026: default parameters should not be used
It's hard to get a default parameter, so you don't need to reload it. Unfortunately, it doesn't conform to the specification.
The default parameter method can be used in CLs, but CLS allows the compiler to ignore the value allocated for these parameters. To maintain the required behavior across programming languages, you must use the method overload that provides default parameters to replace the method that uses default parameters.

7. ca1021: avoid using the out Parameter
In the past, I used to add an out parameter to the method, which became non-compliant.
Passing a type by referencing (using out or ref) requires having the experience of using pointers, understanding the differences between the value type and the reference type, and handling methods with multiple return values. In addition, the differences between out and ref parameters are not widely understood.
This is a bit...

8. ca1011: consider passing the base type as a parameter
I used uielement for a parameter. The code checks that dependencyobject can be used. This is a good result.

9. ca1044: the attribute should not be written only.

For example:
Public bool? Ischeckedwheninit
{
Set
{
// Loghelper. Log ("ischeckedwheninit: {0} path: {1}", value, fullpath );
If (_ ischecked! = Value)
{
_ Ischecked = value;
If (_ ischecked = true)
{
Checkallfiles ();
}
Policypropertychanged ("ischecked ");
}
}
}
Although the read-only attribute is acceptable and often needs to be used, the design criterion prohibits the use of write-only attributes. This is because the user is allowed to set a value, but the user is not allowed to view this value, does not provide any security. In addition, if there is no read access, you cannot view the status of the shared object and restrict its usage.

In fact, I am here for convenience.

 

It is good to do code analysis, and some minor problems can be found. Some of the more detailed rules can still be ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.