This article is a new C # novice common mistakes, but in fact, a lot of experienced programmers often make these mistakes, this special collation for your reference. Specific as follows:
1. Error traversing list
, such as the following code:
list<string> strlist =newlist<string>for (int i =0; i<strlist.count; i++) { strlist.removeat (i);}
This code appears to have removed all elements, and actually each call to the RemoveAt method causes the list element index to reflow, resulting in the element not being completely deleted.
Can be changed to:
list<string> strlist =newlist<string>for (int i =0; i<strlist.count; i++) { strlist.removeat (i); I-=1;}
This allows you to completely delete the elements in the list.
2. Errors on C # constants
For example, you write a class library that defines the following constants:
Public Const String str= "First Version";
And in another program reference this class library, if you modify the class library constants, published a new version, then run the previous program, you will find that the constant or the original constant, and did not change. This is because C # at compile time, constants are embedded directly as metadata, and the workaround is to recompile the entire solution or use properties instead of directly accessing the constants.
3, when the value type is boxed, if the unpacking can only be broken into the original type before boxing
Like what:
Int32 a=3;object obj=new Object ();//The box is successful and will not fail obj=i;//unboxing will fail Int64 b= (Int64) obj;
You can do this like this:
Int64 B = (Int64) (Int32) obj;
To complete the transformation,
4. Error of overloading = = Operator:
Using system;using system.collections.generic;using system.linq;using system.text;namespace UseOperator{ class Program { static void Main (string[] args) { Test t1 = new Test (); T1. Myfun (); Console.ReadLine (); } } Class Test {public void Myfun () { Test t = new Test (); if (t = = null) { Console.WriteLine ("T is empty!) "); } else { Console.WriteLine ("T is empty!) "); } } Overloaded operations with Bugs public static bool operator = = (Test t1, test T2) { return t2. Equals (t1); } public static bool Operator! = (test t1, test T2) { return! ( T1 = = t2); } Overwrite hashcode public override int GetHashCode () { return base. GetHashCode (); } public override bool Equals (object obj) { return base. Equals (obj);}}}
The problem here is that Myfun will pass null into the = = operator function, resulting in error when running, the correct way is:
public static BOOL operator = = (Test t1, test T2) { if ((t2 as Object) = = null) { return (T1 as object) = = NULL; } else { return t2. Equals (t1);} }
5. The property or method of the call structure in C # must declare the struct variable with new, otherwise an error occurs.
6. If you use params with multiple parameters, you must determine whether the parameter is empty, or the program will have hidden bugs.
7, static members will be initialized when the first instance is created, and only be initialized once, do not mess with static members.
8. If using the Ref object type parameter to accept the string type error, because C # requires that the parameter must use the correct type, no ref is possible, if you must use Ref object to accept the string type argument, you can first convert to object, then reference delivery.
9. Never call virtual methods in the constructor of a class, for example:
Using system;using system.collections.generic;using system.linq;using system.text;namespace FransferVirtualFunction { class program { static void Main (string[] args) { try {child ch = new Child (); } catch (Exception ex) { Console.WriteLine (ex. Message); } Console.read (); } } public class Ref {public string Str = "This is a member of the Ref class"; } public class Parent { protected Ref my; Public Parent () { my = new Ref (); The virtual method Console.WriteLine (GetString ()) is called in the constructor method; Virtual method Public virtual String GetString () { return my. STR; Use internal member } } public class Child:parent { private Ref my2; Public Child () : Base () { my2 = new Ref (); } Override the virtual method public override String GetString () { return my2. STR; Use internal member}}}
Here, when executing the constructor of the base class, it executes to the virtual method of the derived class, GetString (), in the Get my2. The exception is thrown when STR, because the derived class object is not yet constructed.
10, in C # and SQL Server communication should pay attention to the meaning of NULL, in SQL Server this value represents 1900-1-1. Null values for SQL Server can be represented by using DBNull.
So much for the time being, notice that the above 10 points can reduce a lot of bugs when programming.
Add:
1. Math trigonometric Functions The parameters are radian values, not angular values.
2, WinForm in the relative path caused by the bug: Specifically, you can refer to
。
3, when using XML, JSON and other serialized data format to pass data, if the data passed is a numeric type, parsing, it is best to first convert it to a string and then tryParse into the corresponding type.
As for the reason: As above the 3rd, is the problem of packing and unpacking.
It is believed that this article can bring great help to the programming of C #.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Summary of common mistakes made by new C # newbies
This address: http://www.paobuke.com/develop/c-develop/pbk23602.html
Related content convert any type of generic collection to an instance of SQLXML data format in C # simple way to access SQLite database (installation, connection, query, etc.) C#.net ArrayList methods of using WinForm to implement the effect of customizing the lower right corner
C # to display all picture files under a folder C # implementation Crawl high-definition beauty sister paper a few practical tips in WinForm a summary of the differences between classes and structures in C #
Summary of common mistakes made by new C # newbies