"Effective C # Essence" selected

Source: Internet
Author: User
Tags foreach format string tostring
Yesterday bought a "effective C #", saw a few item, although not originally read "Effective C + +" when the shock, but also harvested a lot. Put the key points in the following, some provisions add their own understanding, the right as a reading note:-)

Item 1:always Use Properties Instead the Accessible Data members

This is a term that everyone on earth knows. You need to remember that attributes are the external interface part of a class, while (public) members are internal implementations. If the internal implementation is exposed to the outside, it will be very detrimental to the subsequent implementation of the class changes.

Item 2:prefer readonly to const

This article needs to pay attention to a few points:

(1) The Const function occurs at compile time, that is, the compiler replaces all const members with the corresponding constant "value".

(2) The value of a const member is hard-coded in this assembly even if it refers to a const member in another assembly.

(3) The readonly is evaluated at run time, so its performance is slightly worse than the const, but the flexibility is higher.

(4) A const value must be determined at compile time, so it cannot be assigned a value using new.

(5) Updating a public const member's value should be treated as an interface change, while updating the value of a readonly variable can be considered a change in the internal implementation.

Item 3:prefer the IS or as Operators to casts

(1) is or as called "Dynamic Transformation" is attempted and, if it fails, does not throw an exception. Use the AS operator whenever possible. This mechanism uses metadata completion capabilities.

(2) cast is called a "cast", and if it fails, throw an exception--costly.

(3) The IS, as, and cast conversions do not invoke custom conversion operators.

(4) Is can determine whether an object is a value type, and as not.

(5) Please note the Type.isassignablefrom () and Type.issubclassof () methods, they are also commonly used "type detection" means. Note that the Type.issubclassof () method does not support interface detection and TYPE.ISASSIGNABLEFROM () support.

Item 4:use Conditional Attributes Instead of #if

Using #if often (possible) causes performance problems (such as empty method calls) and programs #endif块代码的依赖问题 to #if/.

(1) A method decorated with conditional attributes is always compiled into the target assembly, whether release or Debug.

(2) If the condition does not meet the criteria specified by the conditional attributes, the compiler ignores all calls to the method that it modifies.

(3) The method modified by conditional attributes must return void, which makes sense. Because our program runs cannot rely on the return value of a method that is conditional attributes decorated. Otherwise, under different conditions, our program will show the behavior that is not what we expect.

Item 5:always provide ToString ()

On this point, I have experience in previous projects. For example, once we had to bind the customer list out of the database to ComboBox, we didn't rewrite the ToString () method when we first designed the customer, so we're going to do this:

Select all valid users from the database
String wherestr = String. Format ("where {0} = ' 1 '", customer._isvalid);
Customer[] Customers = (customer[]) dataentrance.getobjects (typeof (Customer), wherestr);
ArrayList cusnamelist = new ArrayList ();
foreach (Customer cus in Customers)
{
Cusnamelist.add (String. Format (' {0} {1} ', Cus.id, Cus. Name)) ;
}
Binding
This.comboBox1.DataSource = cusnamelist;

If the ToString () method is overridden for customer,

#region ToString
public override string ToString ()
{
Return this.id. ToString () + "" + this. Name.tostring ();
}
#endregion

You only need to do this:

String wherestr = String. Format ("where {0} = ' 1 '", customer._isvalid);
Customer[] Customers = (customer[]) dataentrance.getobjects (typeof (Customer), wherestr);
This.comboBox1.DataSource = Customers;

This is a lot easier, and there's a benefit to doing so, for example, when you choose a customer from ComboBox:

String cusid = This.comboBox1.SelectedItem.ToString (). Split (") [0];
Customer descus = null;
foreach (Customer cus in Customers)
{
if (cus.id = cusid)
{
Descus = cus;
break;
}
}
Now, it's much simpler and one line of code is done.

Customer Descus = This.comboBox1.SelectedItem as customer;

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.