20 article. NET coding habits

Source: Internet
Author: User

1, do not hard-string/numeric, you can use some constants instead. (Improve readability)

int Count;
Count = 100;
private static const int ZERO = 0;
if (Count = = ZERO)
{
Perform some actions
}

2. For string comparisons-use string. Empty, not "".

3. Do not declare member variables to be public or proteted, use private member variables and public/protected properties as much as possible. Modify

4. When we want to manipulate strings in a loop, use StringBuilder instead of strings, as shown below.

A bad habit:

String temp = String.Empty;
for (int i = 0; i<=; i++)
{
Temp + = i.ToString ();
}

The habit of better:

StringBuilder sb = new StringBuilder ();
for (int i = 0; i<=; i++)
{
Sb. Append (i.ToString ());
}

5, simple operation, more inclined to use array than collection. (This is recommended, as appropriate)

6, more inclined to use generic Collection than ArrayList. (This is recommended, as appropriate)

7, more inclined to use generic Dictionary than Hashtable. (This is recommended, as appropriate)

8, for the operation and storage of strings, tend to stringcollection and stringdictionary. (This is recommended, as appropriate)

9. Use the appropriate data type.

For example: you want to judge the state, using BOOL is better than int.

A bad habit:

int Check = 0;
if (Check = = 0)
{
Perform some actions

}

The habit of better:

BOOL Check = false;
if (! Check)
{
Perform some actions
}

10, when using as the type conversion, the converted value is judged by the null value

Class A
{

}
Class B:a
{

}
b OBJB = new B ();
A objA1 = (a) objb;
A objA2 = OBJB as A;
if (objA2! = null)
{
Perform the required actions
}

11. Create a WCF proxy, you can use the using expression. (This can be used in many places)

Using (Cerate the proxy)
{
Perform the required actions
}

12, for expensive resources (such as connection, File, etc.), follow the ' acquire late, release early ' (as late as possible to obtain, as early as possible release) guidelines.

Example: If you want to use the SqlConnection object during data manipulation, create an instance at the method level instead of at the class level.

Code class MyData
{
Public MyData ()
{
}
Public list<customer> Getallcustomer ()
{
using (SqlConnection objconnection = new SqlConnection ("Connection string"))
{
Perform some operations to get the required data
}

}
}

If you want to create a class-level SqlConnection instance, make sure that your class implements the IDisposable interface and cleans up the SqlConnection instance in Dispose ().

Code class Mydata:idisposable
{
SqlConnection objconnection;
Public MyData ()
{
objconnection = new SqlConnection ("Connection string");
}
Public list<customer> Getallcustomer ()
{
Get the data you need through objconnection
}
public void Dispose ()
{
Cleaning up SqlConnection instances
if (objconnection! = null)
{
if (objconnection.state = = ConnectionState.Open)
{
Objconnection.close ();
}
}
}
}

13. If you don't want others to extend your class functionality, use ' sealed '.

14. Avoid declaring ' destructor ' for each class, as it increases the life cycle of classes that do not require resident memory.

15, relative manual threading, more inclined to use thread Pool.

16, do not call other methods within the loop. (Call function has performance loss)

For example:

A bad habit:

for (int i = 0; i<=; i++)
{
Calculate (i);
}

The habit of better:

for (int i = 0; i<=; i++)
{
Write calculate logic directly.
}

17, do not handle the exception in the loop, but the loop processing logic in the Try/catch inside

A bad habit:

for (int i = 0; i<=; i++)
{
Try
{
}
catch (Exception ex)
{
Throw ex;
}
}

The habit of better:

Try
{
for (int i = 0; i<=; i++)
{
}
}
catch (Exception ex)
{
Throw ex;
}

18. Application logic without exception handling

For example:

A bad habit:

Try
{
int x, y, Z;
x = 0;
y = 10;
z = y/x;
}
catch (Devidebyzeroexception ex)
{
Throw ex;
}

The habit of better:

Try
{
int x, y, Z;
x = 0;
y = 10;
if (x! = 0)
{
z = y/x;
}
}
catch (Exception ex)
{
}

19, relative for/while, inclined to use the Foreach loop. Correct

20, the use of multi-layered architecture system, layer-to-layer interaction, compared to dataset/datatables more inclined to use the object to pass data.

20 article. NET coding habits

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.