


C# learning record: Writing high-quality code and improving organization suggestions 4-8
4. TryParse is better than Parse
The following TryParse method definition
public static bool TryParse(string s, out Double result);
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); }
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;
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
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.
