C # Programming doubts (III.)

Source: Internet
Author: User

1, static class can only have static members (including static fields, static methods), can not contain non-static variables. A static class cannot have a constructor function.

2, static class inside can declare const variable and do not add static before, because the default const variable default is static, it belongs to the class itself. ReadOnly is not static, so we use the static readonly variable when we write connectionstring in the database tool class.

3, before I write the method, if the return is bool but inside the return value of the method operation is int, I will write in the following code,

string " Select COUNT (1) from Tb_users where id=100 "  New  SqlCommand (); int count = cmd. ExecuteNonQuery (); if 0 ) {    returntrue;} return false;

Recently saw a line of code, hehe, only to find the previous trouble, the code is as follows:

string " Select COUNT (1) from Tb_users where id=100 "  New  SqlCommand ();            return 0;

This allows you to simplify a lot of code.

4. How do we determine if an object needs to be applied outside when writing code? There is one aspect of the experience that we can look at whether the object has a Dispose method, if this method or implement the IDisposable interface is best nested in the using, otherwise we need to manually call the Dispose method.

5, we use using to release resources in the code snippet does not have a way to block the current process, such as do not in the database Sqlreader operation in the MessageBox sub-operation, because it will block the current process, the resources are not released, it is best to write a flag in the outside, Then through the flag bit to judge, otherwise the user stays in the MessageBox screen to do other things when the resources will not be released.

6, static constructors, whether called several times or in several classes are called separately, the static constructor is executed only once, no longer execute.

// class1.i = 0; // The static constructor is then assigned, so open here is 0, otherwise the following 1 Response.Write (CLASS1.I);

C # Programming doubts (III.)

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.