1 Timely release of resources
The CLR hosting environment plays the role of garbage collection, so you do not need to explicitly release the memory occupied by created objects. But that doesn't mean you can ignore all used objects. Many objects encapsulate other types of system resources (eg, disk files, data connections, network ports). Keeping these resources in use can drastically exhaust system resources, impair performance and ultimately cause program errors. When you open a file, network port, or data connection, you should explicitly release the resources as soon as you no longer use them.
In addition, for resource operations, it is generally necessary to add exception capture processing (Try..Catch). At this time, don't forget to release the resource in finally to ensure that the resource can be released normally when catching the exception.
2 Correctly stop multi-threading
FileStream fs = File.Open(…);
Try{…} Finally{ fs.Close;}
Assume that the above code is in the working thread and has reached finally Inside, at this time the UI thread calls the thread's Abort() method, so it is very likely that the worker thread will jump out of the finally code block before fs.Close is executed. This way your fs will never be Closed.
In most cases, finally will be executed forever, but does not include ThreadAbortException caused by calling Thread.Abort. For this reason, it is not recommended to use Abort.
To correctly stop a thread, it does not depend on what behavior the caller takes (do not use Thread.Abort() directly), but more on whether the worker thread can actively respond to the caller's stop request.
The general mechanism is that if the thread needs to be stopped, then the thread itself should be responsible for opening the Cancel interface to the caller.
3 Type conversion related
If a value is read from the database, it will be of type int when there is data. If there is no data, it will be null. If the type is forced, an exception will occur. Therefore, forced transfer is rarely used. If used, an exception must be caught to avoid program exceptions.
In the case where the forced transfer is not good, we recommend using the TryParse method, which has already performed exception handling on the Parse method.
You can also use Convert, which also requires exception capture; in fact, wherever type conversion, serialization and other operations are involved, exceptions need to be captured;
4 String operation issues
When operating on strings , if a large number of splicing operations are involved, it is recommended to use StringBuilder. If you use String, there will be obvious performance losses. The reason is that the string object is a very special object and cannot be changed once it is assigned a value. Calling any splicing operation (such as assignment, "+", etc.) in the String class at runtime will create a new string object in the memory, which also means that new memory space must be allocated for the new object.
5 Problems caused by const constant modification
Pay special attention when the program references const constants in other dlls.
If you modify the const constant in this dll, you must recompile all programs that reference the const constant in this dll, otherwise the constant value used in the program will be inconsistent with that in the dl.
In addition, if you use readonly instead of const, you can solve this problem without recompiling, because const is a compiled constant, and readonly is a runtime constant.
6 C# compilation target platform issue
When the compilation target platform of the dll that the program depends on is X86, the compilation target platform of the program itself must also be X86 (instead of the default option Any CPU), otherwise the 64-bit computer will Can not operate.
7 Cross-thread access control
When developing interface programs, you will encounter time-consuming operations. For the friendliness of the program, we generally perform time-consuming operations in the task thread and display the execution information in Main UI thread.
If you operate the controls in the main UI thread directly in the task thread, it is very easy to cause an exception and report "the value of the thread that created the control cannot be modified in other threads". If it is set to prohibit the compiler from checking cross-thread access , no error will be reported, but unpredictable problems will occur. At this time, it is recommended to use delegation or anonymous delegation.
The above is the detailed content of Several common mistakes in C#/.NET. For more information, please follow other related articles on the PHP Chinese website!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment