C # Learning Record: Writing high-quality code improvement recommendations 4-8

Source: Internet
Author: User
4. TryParse is better than parse

The following method defines the TryParse

public static bool TryParse (string s, out Double result);

Parse if the conversion fails, but TryParse has a return value to determine whether the conversion succeeded

String str1 = "Abfc12", or if (double. TryParse (str1, out double dou1)) {    Console.WriteLine (DOU1);}

5, the proposed use of int? To ensure that a value type can also be null

If there is a need for a value type of NULL, we may use a special value such as-one to determine if an int is empty, preferably an int? Type, which determines whether it is null

nullable<int> I1 = 4;//i2 and i1 are defined in the same way as just a different int? Is it a syntactic sugar int? I2 = null;int i3 = 0;//int type can be converted to int by default? Type i2 = I3;//int? The type needs to be strongly converted to an int type, or 0i3 = (int) i2 if null;

6. How to use the difference between readonly and const

The simple difference is the high efficiency of the const, readonly flexibility

Const is a compiler constant, ReadOnly is a run-time

const int constnum = 1;public string name;public int age;public firsttype () {    name = "AA";    age = Constnum;    age = 1;} Using the above code test, the following compiled IL code  il_0000:  ldarg.0  il_0001: Call       instance void [mscorlib]System.Object: :. ctor ()  il_0006:  nop  il_0007:  nop  il_0008:  ldarg.0  il_0009:  ldstr      "AA"  il_000e:  stfld      string csharpsuggest.firsttype::name  il_0013:  ldarg.0  il_0014 :  ldc.i4.1  il_0015:  stfld      int32 csharpsuggest.firsttype::age  il_001a:  ldarg.0  il_001b:  ldc.i4.1  il_001c:  stfld      int32 csharpsuggest.firsttype::age  il_0021:  ret

It can be seen that 13,14,15 and 1a,1b,1c are the same, so age = constnum; and age = 1; it's equivalent, so it's the most efficient.

Const can only modify primitive types, enum types, or string types, and readonly does not restrict

Const natural is static can no longer add static

The value of readonly is usually assigned within the constructor, and each class object can have a different readonly value, but because const is static, all classes have the same value.

There is this sentence in the book, I always have a question, in the class assignment and constructor assignment is different, no search on the web, the anti-compiled IL code is only the variable definition of the order of the difference, if you know please inform.

7. Use 0 as the default value for enumerations

I understand that if it is not necessary, do not change the value of the enumeration, there may be unexpected results

8. Avoid providing values that are displayed for enumeration type elements

The reason is ditto

Related articles:

C # Learning Record: Writing high-quality code improvement recommendations 1-3

C # Learning Record: Writing high-quality code improvement recommendations 9-15

Related Article

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.