Sometimes, when we do more projects, especially website projects, we will feel that basic knowledge is not very useful, but we do not know that if we do not have a deep understanding of some basic concepts, it will cause problems. Sort out the problems caused by poor understanding of basic concepts in the project, and hope to inspire them.
1. Storage of reference types
We know that the reference type is stored in the heap. When a reference type is assigned to another reference type, the two point to the same object, any modification to a variable affects the value of another variable. If you take this sentence or answer questions during the interview, you can understand it. However, if you do not have a deep understanding of the complex logic added to a specific project, it will cause some problems. The following is a real case of mistakes in the project.
Modification of reference type assignment
Static Void Main ( String [] ARGs)
{
Reftypecache rtc = New Reftypecache ();
RTC. showdatas ( " B " );
RTC. showdatas ( " A " );
RTC. showdatas ( " B " );
Console. Read ();
}
Public Class Reftypecache
{
/// <Summary>
/// Different types of user operation data
/// </Summary>
Public Void Showdatas ( String Usertype)
{
Datatable dt = This . Getuserdata ();
// If a user wants to Display User credits, a column of credits is added to the newly assigned DT value.
If (Usertype = " A " )
{
DT. Columns. Add ( " Score " );
Console. writeline ( " The data obtained by a user includes the credit column: " + DT. Columns. Contains ( " Score " ));
}
// Class B users can display the data directly without accumulating data.
If (Usertype = " B " )
{
Console. writeline ( " The data obtained by Class B users includes the points column: " + DT. Columns. Contains ( " Score " ));
}
}
/// <Summary>
/// Get Data
/// </Summary>
Private Datatable getuserdata ()
{
Datatable dt = Null ;
System. Web. caching. cache Cache = System. Web. httpruntime. cache;
String Cachename = " Alldata " ;
Object O = Cache. Get (cachename );
// Obtain from Cache
If (O ! = Null )
{
DT = O As Datatable;
}
// Initialize data without Cache
If (DT = Null )
{
DT = New Datatable ();
DT. Columns. Add ( " ID " );
DT. Columns. Add ( " Datas " );
Datarow dr0 = DT. newrow ();
Dr0 [ " ID " ] = " 1 " ;
Dr0 [ " Datas " ] = " Zhang San " ;
DT. Rows. Add (dr0 );
Datarow DR1 = DT. newrow ();
DR1 [ " ID " ] = " 2 " ;
DR1 [ " Datas " ] = " Li Si " ;
DT. Rows. Add (DR1 );
Datarow Dr2 = DT. newrow ();
DR2 [ " ID " ] = " 3 " ;
DR2 [ " Datas " ] = " Zhao Liu " ;
DT. Rows. Add (DR2 );
Cache [cachename] = DT;
}
ReturnDT;
}
}
As shown in the preceding example, after an object in the cache is assigned to another variable, the value of the variable is modified, which changes the value of the cached object.Program.
2. Object Instantiation
We know that new classname () is used to instantiate an object. Some variables in the object, such as int type, will have default values. However, if you are not careful about it, problems may occur. A specific instance appears in this way, its pseudoCodeAs follows:
View code
Userentity user = New Userentity (); // User entity
Billentity Bill = New Billentity (); // User money change entity
// if conditions are met, add money to users and generate consumption details
If (condition)
{< br> bill. userid = userid;
bill. setmoney = 5 ;
bill. createtime = datetime. now;
bill. type = 0 ; // type: Complimentary
}
// Modify user information
Userbiz UB = New Userbiz ();
UB. Update (User );
// Add consumption details if the account changes.
// The problem here is that the Bill object is not assigned a value, and the bill is null,
// After ignoring new billentity (), the memory has been allocated to Bill, and the bill variable has stored values.
If (Bill ! = Null )
{
Billbiz bb = New Billbiz ();
BB. Add (Bill );
}
The above two examples are very simple. You may find the problem at once and find it difficult to make such a mistake. However, this problem exists in the development process, if you do not have enough understanding of the basic concepts and complex logic, you may make strange mistakes. net basic knowledge cannot be ignored.