10 C # errors easily made by C + + programmers

Source: Internet
Author: User
Tags requires

We know that C # 's syntax is very similar to C + +, and the transition from C + + to C # is not the language itself, but the familiarity. NET's manageable environment and understanding of the. NET Framework. Although C # and C + + in the grammatical changes are very small, almost no impact on us, but some changes are enough to make some of the careless C + + programmers always remember. In this article, we'll discuss 10 of the easiest mistakes that C + + programmers can make.

Trap 1: No definitive End method

It is almost entirely certain that, for most C + + programmers, C # is the biggest difference from C + + is in the collection of fragments. This also means that programmers no longer have to worry about memory leaks and ensure that all the useless pointers are removed. But we can no longer control precisely the process of killing unwanted objects. In fact, there is no clear destructor in C #.

If you use unmanaged resources, you must explicitly release the resources after you do not use them. Implicit control over resources is provided by the Finalize method (also known as finalizer), and when the object is destroyed, it is invoked by the fragment collector to reclaim the resource occupied by the object. Instead of involving other objects, finalizer should release only the unmanaged resources occupied by the objects being destroyed. If you use only manageable resources in your program, you do not have to or should not execute a Finalize method, which is only used in the handling of unmanaged resources. Because finalizer requires a certain amount of resources, you should only perform finalizer in the method that requires it. The Finalize method that calls an object directly is absolutely not allowed (unless you call finalize of the underlying class in finalize of the subclass). ), the defragmenter automatically invokes finalize.

Syntactically, destructor in C # is very similar to C + +, but in fact they are completely different. destructor in C # is just a shortcut to defining a Finalize method. Therefore, the following two snippets of code are different:

  ~MyClass()
{ // 需要完成的任务
}
   MyClass.Finalize() {// 需要完成的任务
base.Finalize();
}

Who is wrong 2:finalize and dispose to use?

It is clear from the above discussion that the explicit invocation of finalizer is not allowed and can only be invoked by the fragment collector. If you want to free up a limited number of unmanaged resources (such as file handles) that you no longer use, you should use the IDisposable interface, which has a Dispose method that can help you accomplish this task. Dispose is a method that frees unmanaged resources without waiting for finalize to be invoked.

If you have already used the Dispose method, you should prevent the defragmenter from executing the Finalize method on the corresponding object. To do this, you call the static method gc.suppressfinalize and pass the pointer of the corresponding object to it as an argument, and the Finalize method calls the Dispose method. Accordingly, we can get the following code:

public void Dispose()
{
// 完成清理操作
// 通知GC不要再调用Finalize方法
GC.SuppressFinalize(this);
}
public override void Finalize() {
Dispose(); base.Finalize();
}

For some objects, it might be more appropriate to call the Close method (for example, a file object call Close is more appropriate than dispose) by creating a Dispose method of a private property and a close method of the public property. And let close call Dispose to implement the Close method call to some objects.

Because it is not certain that dispose will be invoked and finalizer execution is uncertain (we cannot control when the GC will run), C # provides a using statement to ensure that the Dispose method is invoked as early as possible. The general approach is to define which object to use, then specify an active range for those objects with parentheses, and when the most inner bracket is encountered, the Dispose method is automatically invoked to process the object.

using System.Drawing;
class Tester
{
public static void Main()
{
using (Font theFont = new Font("Arial", 10.0f))
{
//使用theFont对象
} // 编译器将调用Dispose处理theFont对象
Font anotherFont = new Font("Courier",12.0f);
using (anotherFont)
{
// 使用anotherFont对象
} // 编译器将调用Dispose处理anotherFont对象 }
}

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.