I bought a copy of Objective C # yesterday and read several items. Although I was not as shocked as I was when I first read Objective C ++, I also gained a lot. Record the key points and add your own understanding to some terms. Use the power as a Reading Note :-)
Item 1: Always use properties instead of accessible data members
This is what everyone on Earth knows. You need to remember that the attribute is the external interface part of the class, while the (public) member is the internal implementation. If the internal implementation is exposed to the external, it will be very unfavorable for future class implementation changes.
Item 2: prefer readonly to const
Pay attention to the following points:
(1) const takes effect during compilation, that is, the compiler replaces all const members with the corresponding constant "value ".
(2) even if the const member in other assembly is referenced, the const member value is hardcoded in this Assembly.
(3) readonly is evaluated during runtime, so its performance is slightly worse than const, but its flexibility is higher.
(4) The const value must be determined during the compilation period, so it cannot be assigned a value using new.
(5) Updating the value of a public const member should be regarded as an interface change, while updating the value of a readonly variable can be considered as a change of internal implementation.
Item 3: prefer the is or as operators to casts
(1) "is" or "as" is called "dynamic conversion". It is a trial. If it fails, no exception will be thrown. Use the as operator whenever possible. This mechanism uses metadata to complete the function.
(2) cast is called "forced conversion". If it fails, an exception is thrown-it is expensive.
(3) is, as, and cast conversions do not call custom conversion operators.
(4) is can be used to determine whether an object is of the value type, but as cannot.
(6) Pay attention to the type. isassignablefrom () and type. issubclassof () methods. They are also common "type detection" methods. Note that the type. issubclassof () method does not support interface detection, whereas the type. isassignablefrom () method does.
Item 4: Use conditional attributes instead of # If
The use of # If often (possibly) causes performance problems (such as empty method calls) and program dependencies on # If/# endif block code.
(1) The method modified using conditional attributes will always be compiled into the target assembly, whether it is release or debug.
(2) If the condition does not meet the condition specified by the conditional attributes, the compiler ignores all calls to the methods it modifies.
(3) The method modified by conditional attributes must return void, which makes sense. Because the program running cannot depend on the return value of the method modified by conditional attributes. Otherwise, under different conditions, our program will show unnecessary behavior that is not what we expect.
Item 5: always provide tostring ()
I have learned about this in my previous projects. For example, we used to bind the list of customers retrieved from the database to ComboBox. At the beginning, we didn't rewrite the tostring () method when designing the customer, so we need to do this:
// Pick out all valid users from the database
String wherestr = string. Format ("Where {0} = '1'", customer. _ isvalid );
Customer [] MERs = (customer []) dataentrance. getobjects (typeof (customer), wherestr );
Arraylist cusnamelist = new arraylist ();
Foreach (customer Cus in mers)
{
Cusnamelist. Add (string. Format ("{0} {1}", Cus. ID, Cus. Name ));
}
// Bind
This. combobox1.datasource = cusnamelist;
If the tostring () method is rewritten for the 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 [] MERs = (customer []) dataentrance. getobjects (typeof (customer), wherestr );
This. combobox1.datasource = MERs MERS;
This is much easier, and there is another benefit to doing so. For example, when choosing a customer from ComboBox, we need to do this before:
String cusid = This. combobox1.selecteditem. tostring (). Split ('') [0];
Customer descus = NULL;
Foreach (customer Cus in mers)
{
If (cusid. ID = cusid)
{
Descus = cuz;
Break;
}
}
Now it's much simpler, just get a line of code: Customer descus = This. combobox1.selecteditem as customer;