C # harm of reference type & quot;

Source: Internet
Author: User

The front-end time has just completed a project and finally has time to summarize and review it.

Project requirements: send emails to users. emails are classified into system emails and personal emails, which need to be sorted by time and unread in descending order.

At first, I thought this was a simple requirement. I created an object for the email:

Class Mail {

Private string _ title = string. Empty;

Public string Title {

Get {return _ title ;}

}

Private string _ message = string. Empty;

Public string Message {

Get {return _ message ;}
}

Private bool _ isRead = false;

Public bool IsRead {

Get {return _ isRead}

}

Privaite DateTime _ insertTime = DateTime. Now;

Public DateTime InsertTime {

Get {return _ insertTime ;}

}

}

Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)

For (int I = 0, count1 = listMails. Count; I <count1; I ++ ){

For (int j = I + 1; j <count1; j ++ ){

If (listMails [I]. IsRead &&! ListMails [j]. IsRead ){

Mail temp = listMails [I];

ListMails [I] = listMails [j];

ListMails [j] = temp;

}

}

}

The results can be imagined and are not arranged in descending order of unread.

Method 1: perform in-depth copy of Mail:

[Serializable]

Class Mail: ICloneable {

Private string _ title = string. Empty;

Public string Title {

Get {return _ title ;}

}

Private string _ message = string. Empty;

Public string Message {

Get {return _ message ;}
}

Private bool _ isRead = false;

Public bool IsRead {

Get {return _ isRead}

}

Privaite DateTime _ insertTime = DateTime. Now;

Public DateTime InsertTime {

Get {return _ insertTime ;}

}

Public object Clone ()
{
Using (Stream objectStream = new MemoryStream ())
{
IFormatter formatter = new BinaryFormatter ();
Formatter. Serialize (objectStream, this );
ObjectStream. Seek (0, SeekOrigin. Begin );
Return formatter. Deserialize (objectStream) as Employee;
}
}

}

Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)

For (int I = 0, count1 = listMails. Count; I <count1; I ++ ){

For (int j = I + 1; j <count1; j ++ ){

If (listMails [I]. IsRead &&! ListMails [j]. IsRead ){

Mail temp = (Mail) listMails [I]. Clone ();

ListMails [I] = (Mail) listMails [j]. Clone ();

ListMails [j] = temp;

}

}

}

The result still does not work. It is depressing.

 

Finally, the value type (struct) is used for processing:

Struct Mail {

Private string _ title;

Public string Title {

Get {return _ title ;}

}

Private string _ message;

Public string Message {

Get {return _ message ;}
}

Private bool _ isRead;

Public bool IsRead {

Get {return _ isRead}

}

Privaite DateTime _ insertTime;

Public DateTime InsertTime {

Get {return _ insertTime ;}

}

}

Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)

For (int I = 0, count1 = listMails. Count; I <count1; I ++ ){

For (int j = I + 1; j <count1; j ++ ){

If (listMails [I]. IsRead &&! ListMails [j]. IsRead ){

Mail temp = listMails [I];

ListMails [I] = listMails [j];

ListMails [j] = temp;

}

}

}

Test, all OK, we recommend that all students have a deep understanding of the value type and reference type to avoid unnecessary trouble.

Reprinted, please indicate the source, mobile game base www.shouyou888.com, QQD123 game network www.qqd123.com

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.