Home > Backend Development > C#.Net Tutorial > C# learning record: Writing high-quality code and improving organization suggestions 4-8

C# learning record: Writing high-quality code and improving organization suggestions 4-8

php是最好的语言
Release: 2018-08-06 14:56:55
Original
1476 people have browsed it

4. TryParse is better than Parse

The following TryParse method definition

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

Parse will report an error if the conversion fails, but TryParse has a return value. Determine whether the conversion is successful

string str1 = "abfc12";
if(double.TryParse(str1, out double dou1))
{
    Console.WriteLine(dou1);
}
Copy after login

5. It is recommended to use int? to ensure that the value type can also be null

If the value type needs to be null, we may use a special value such as -1 to To determine whether int is empty, it is best to change it to int? Type, determine whether it is null

Nullable<int> i1 = 4;
// i2 和 i1 的定义方式一样 只是写法不同 下面的int?是一个语法糖
int? i2 = null;
int i3 = 0;
//int类型可以默认转为int?类型
i2 = i3;
//int?类型需要强转成int类型,如果是null则变为0
i3 = (int)i2;
Copy after login

6. How to use readonly and const

The simple difference is that const is more efficient and readonly is more flexible

const is a compiler constant , readonly is a runtime constant

const int constNum = 1;
public string name;
public int age;
public FirstType()
{
    name = "aa";
    age = constNum;
    age = 1;
}
//使用以上代码测试,下面的编译成的IL代码
  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
Copy after login

It can be seen that 13, 14, 15 are the same as 1a, 1b, 1c, so age = constNum; and age = 1; are equivalent and therefore the most efficient

const can only modify primitive types, enumeration types or string types, readonly has no restrictions

const is naturally static and cannot be added to static

The value of readonly is generally in the construction When assigning values ​​in the function, each class object can have a different readonly value, but since const is static, the value is the same for all classes

C# learning record: Writing high-quality code and improving organization suggestions 4-8

Book In the above sentence, I have always had a question, what is the difference between assignment in the class and assignment in the constructor? I can't find it online. The decompiled IL code is only the difference in the order of variable definition. If you know, please let me know. .

7. Use 0 as the default value of the enumeration

What I understand is that if it is not necessary, do not change the value of the enumeration, as unexpected results may occur

8. Avoid providing displayed values ​​for enumeration type elements

The reasons are the same as above

Related articles:

C#Learning records: Writing high-quality code improvement suggestions 1-3

C# Learning Record: Suggestions for writing high-quality code and improving it 9-15

The above is the detailed content of C# learning record: Writing high-quality code and improving organization suggestions 4-8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template