Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems that beginners in C # are easy to make?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what are the problems that C# novices are easy to commit". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn what are the problems that are easy to be made by beginners in C#.

1 release resources in a timely manner

The CLR managed environment plays the role of garbage collection, so you do not need to explicitly free the memory occupied by the created objects. But that doesn't mean you can ignore all used objects. Many objects encapsulate other types of system resources (for example, disk files, data connections, network ports). Keeping these resources in use will sharply deplete the system's resources, weaken performance and eventually lead to program errors. When you open a file, network port, or data connection, when you no longer use these resources, you should explicitly release these resources as soon as possible.

In addition, for the operation of resources, you generally need to add exception catch handling (Try..Catch). Don't forget to release resources in finally to ensure that resources can be released normally when an exception is caught.

2 stop multithreading correctly

FileStream fs = File.Open (…)

Try {... } Finally {fs.Close;}

Assuming that the above code is in the worker thread and is already in the finally, and the UI thread calls the thread's Abort () method, it is very likely that the worker thread will jump out of the finally code block before the fs.Close is executed. So your fs will never be Close.

In most cases, finally is executed forever, but does not include ThreadAbortException exceptions that are thrown by calling Thread.Abort. For this reason, Abort is not recommended.

Stopping a thread correctly depends less on what behavior the caller takes (don't use Thread.Abort () directly), and 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, the thread itself should be responsible for opening up the Cancel interface to the caller.

3 related to type conversion

If a value is read from the database, it is of int type when there is data, and null is obtained if there is no data, and an exception will occur if the type is overturned. Therefore, strong transfer is rarely used, and if used, we must also do an exception catch to avoid program exceptions.

In cases where strong conversion is not good, we recommend using the TryParse method, which already has exception handling for the Parse method.

You can also use Convert, which also needs to catch exceptions; in fact, exceptions need to be caught wherever operations such as type conversion, serialization, etc.

4 string operation problems

In string operations, it is recommended to use StringBuilder if a large number of splicing operations are involved. If you use String, it will bring obvious performance loss. The reason is that the string object is a very special object that cannot be changed once it is assigned. Calling any stitching operation in the String class at run time (such as assignment, "+", and so on) creates a new string object in memory, which also means that new memory space is allocated for the new object.

5 problems caused by modification of const constant

Pay special attention when programs refer to const constants in other dll.

If you modify the const constant in this dll, you should 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 dl.

In addition, if you use readonly instead of const to solve this problem, you don't need to recompile, because const is a compiled constant and readonly is a runtime constant.

6 problems with the target platform of C# compilation

When the program depends on the dll compilation target platform is X86, the program itself compilation target platform must also be X86 (rather than the default option Any CPU), otherwise the 64-bit computer will not be able to run.

7 access to controls across threads

When developing interface programs, we will encounter more time-consuming operations. For the sake of program friendliness, we usually perform time-consuming operations in the task thread and display the execution information in the main UI thread.

If the control in the main UI thread is operated directly in the task thread, it is extremely prone to exceptions, reporting that "the value of the thread that creates the control cannot be modified in other threads". If the compiler is set to prohibit the compiler from checking cross-thread access, it will not report an error, but there will be unpredictable problems. At this point, it is recommended to use delegate or anonymous delegate.

At this point, I believe that everyone has a deeper understanding of the "C# novice easy to commit the problem", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report